继承的方式完成包装__attr__

__getattr__    当实例要调用的属性不存在的时候会触发

__setattr__    当实例要设置属性的时候会触发

__delattr__    当实例要删除属性的时候会触发

这三个方法是python内置的方法,如果不自己定义的话,就是默认的,自己定义的话就是用自己定义的

1、__getattr__           当要调用的属性不存在的时候会触发

没有自己定义__getattr__的时候

class Foo:
    def __init__(self,name):
        self.name=name
f1=Foo("gouguoqi")
print(f1.name)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

gouguoqi

我们自己定义一个__getattr__

class Foo:
    def __init__(self,name):
        self.name=name
    def __getattr__(self, item):
        print("你要找的属性%s不存在" %item)
f1=Foo("gouguoqi")
print(f1.name)
print(f1.age)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

gouguoqi

你要找的属性age不存在

2、__setaddr__    给属性设置值的时候会触发

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

2.1我们在没有自己定义__setattr__的时候,系统是帮我们把属性设置到属性字典里面

class Foo:
    def __init__(self,name):
        self.name=name
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

{'name': 'gouguoqi'}

2.2但是我们自己定制的那个setattr只是执行了一个打印的操作,并没有帮我们把值新增到属性字典里面去

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr
f1.age=28
f1.gender="male"
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

执行setattr

执行setattr

{} 

2.3现在有一个需求,传入的值必须是字符串,应该怎么实现

先完成以下判断逻辑,然后在具体的去实现功能

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{}

实现设置功能,只能是字符串才能设置进去

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
            self.__dict__[k]=v
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

 

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{'name': 'gouguoqi'}

2.4还可以实现。如果是字符串的话,把字符串变为大写

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
            self.__dict__[k]=v.upper()
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{'name': 'GOUGUOQI'}

3、__delattr__  删除属性

class Foo:
    def __init__(self,name):
        self.name=name
    def __delattr__(self, item):
        print("执行delattr",item)
        self.__dict__.pop(item)#操作底层的属性字典的key
f1=Foo("gouguoqi")
del f1.name#把f1传给self,把name传给item
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行delattr name

{}

3.1那么系统默认的删除就是这样的啊,我们为什么要自己定义一个这个呢,那么又来一个新的需求,所有的属性都不能删除,改怎么做呢

class Foo:
    def __init__(self,name):
        self.name=name
    def __delattr__(self, item):
        print("%s属性不允许删除" %item)
f1=Foo("gouguoqi")
del f1.name
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

name属性不允许删除

{'name': 'gouguoqi'

4、包装 

class List(list):#定义个类,继承系统的list
    pass

l2=list("hello world")
print(l2,type(l2))

l1=List("hello world")
print(l1,type(l1))

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] <class 'list'>

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] <class '__main__.List'>

4.1现在想要实现的是 定制一个方法,显示出列表中间元素的那个元素内容

class List(list):#定义个类,继承系统的list
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
print(l1,type(l1))
print(l1.show_middle())

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'] <class '__main__.List'>

w

4.2我们自己定义一个apped的方法

结果就是append这个方法不能追加了。因为自己定义的是pass

class List(list):#定义个类,继承系统的list
    def append(self, object):
        pass
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append(111)
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

实现真正的append操作

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            list.append(self,object)#用父类的append方法
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("111")
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '111']

类调用方法,推荐用super

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            # list.append(self,object)#用父类的append方法
            super().append(object)#super就是调用父类的方法,不用传self
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("222")
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '222']

4.3这样就实现了  append我们自己定制了一个,只能添加字符串,不能append数字

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            # list.append(self,object)#用父类的append方法
            super().append(object)#super就是调用父类的方法,不用传self
        else:
            print("只能append字符串")
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("222")
print(l1)
l1.append(666)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '222']

只能append字符串

posted @ 2018-06-22 21:27  gouguoqi  阅读(280)  评论(0编辑  收藏  举报