python getattribute、get、getattr、getitem等用法
__getattribute__
__getattribute__是属性访问拦截器,就是当类的属性被访问时,会自动调用类的__getattribute__方法。
参考代码:
class Attribute(object): def __init__(self): self.name = '247gzs' self.age = 24 self.sex = '男' def __getattribute__(self, item): print("__getattribute__() is called", item) return object.__getattribute__(self, item)if __name__ == '__main__': attribute = Attribute() print(attribute.name) # output__getattribute__() is called name247gzs代码执行过程中,当调用实例对象attribute的name属性时,不会直接打印,而是把name的值作为实参传进__getattribute__方法中,经过一系列操作后,再把name的值返回。
python中只要定义了继承object的类,就默认存在属性拦截器,只不过是拦截后没有进行任何操作,而是直接返回。
我们可以自己改写__getattribute__方法来实现相关功能,比如查看权限、打印log日志等。
getattr、hasattr、setattr
对象属性的判断。
参考代码:
class Attribute(object): passif __name__ == '__main__': attribute = Attribute() print('-------- 未设置 --------') print(hasattr(attribute, 'name')) # 判断是否存在属性 print(hasattr(attribute, 'age')) setattr(attribute, 'name', '247gzs') # 设置属性值 setattr(attribute, 'age', 24) print('-------- 已设置 --------') print('-------- 判断是否存在 --------') print(hasattr(attribute, 'name')) print(hasattr(attribute, 'age')) print('-------- 获取属性值 --------') print(getattr(attribute, 'name')) # 获取属性值 print(getattr(attribute, 'age')) print(getattr(attribute, 'sex', '男')) # output-------- 未设置 --------FalseFalse-------- 已设置 ---------------- 判断是否存在 --------TrueTrue-------- 获取属性值 --------247gzs24男__getattr__、__setattr__、__delattr__
类支持.操作来访问属性;定制功能:耗时、日志等等。
参考代码:
class Attribute(object): """ 使类支持 . 操作,默认情况下,类支持 . 操作 """ def __getattr__(self, item): print("__getitem__() is called", item) if item in self.__dict__: return self.__dict__[item] def __setattr__(self, key, value): print("__setitem__() is called", key, value) self.__dict__[key] = value def __delattr__(self, item): print("__delitem__() is called", item) del self.__dict__[item]if __name__ == '__main__': attribute = Attribute() attribute.name = '247gzs' attribute.age = 24 print(attribute.name, attribute.age) del attribute.name # print(attribute['age']) # 报错,不支持此方法 # output__setitem__() is called name wxs__setitem__() is called age 24247gzs 24__delitem__() is called name__getitem__、__setitem__、__delitem__
类支持通过[]来访问属性
参考代码:
class Attribute(object): """ 给类添加 [] 操作 """ def __getitem__(self, item): print("__getitem__() is called", item) if item in self.__dict__: return self.__dict__[item] def __setitem__(self, key, value): print("__setitem__() is called", key, value) self.__dict__[key] = value def __delitem__(self, key): print("__delitem__() is called", key) del self.__dict__[key]if __name__ == '__main__': attribute = Attribute() attribute['name'] = '247gzs' print(attribute['name']) del attribute['name'] # output__setitem__() is called name wxs__getitem__() is called name247gzs__delitem__() is called name__get__、__set__、__delete__
参考代码:
# 定义描述符class Attribute(object): def __init__(self, key, value_type): """ :param key: 用来操作底层属性字典 :param value_type: 用来表示期望的数据类型 """ self.key = key self.value_type = value_type def __get__(self, instance, owner): print('执行了__get__') return instance.__dict__[self.key] # return p2.name def __set__(self, instance, value): print('执行了__set__', self) if not isinstance(value, self.value_type): # 用来判断用户传入的是否符合要求 raise TypeError('%s 传入的不是 %s' % (self.key, self.value_type)) # 抛出类型异常,提示用户程序终止 instance.__dict__[self.key] = value # 符合要求,则设置属性对应的值 def __delete__(self, instance): print('执行了__delete__') instance.__dict__.pop(self.key)# 定义一个人的类(被代理的类)class People(object): name = Attribute('name', str) # 用描述符代理了name这个属性,相当于执行了Attribute中的self.__set__ age = Attribute('age', int) salary = Attribute('salary', float) def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salaryperson = People('Wxs', 24, 11.2)# 访问print(person.name)# 赋值person.name = '247gzs'person.age = 25# output执行了__set__ 执行了__set__ 执行了__set__ 执行了__get__247gzs执行了__set__ 执行了__set__参考文档:
[1] https://blog.csdn.net/yiifaa/article/details/78068962[2] https://www.cnblogs.com/flashBoxer/p/9771797.html[3] https://www.cnblogs.com/andy1031/p/10923834.html[4] http://blog.itpub.net/26736162/viewspace-2643143/[5] https://blog.csdn.net/yitiaodashu/article/details/78974596 (__getattribute__)[6] https://www.cnblogs.com/Meanwey/p/9898222.html (__get__、__set__、__delete__)[7] https://www.v2ex.com/t/511140[8] https://blog.csdn.net/yiifaa/article/details/78068962