python property __getattr__ __getattribute__
class MyCls(): def __init__(self,age,name): self._age=age self.name=name @property def age(self): return self._age @age.setter def age(self,value): self._age=value def __getattr__(self, item):##查找不到相关属性时会执行此方法 return "notattr" def __getattribute__(self, item):##有没有查找到相关属性都会先执行此方法 获取对象的所有的属性值都是firstnotattr return "firstnotattr" mycls=MyCls(12,"sssss") print(mycls.age)##12 mycls.age=23 print(mycls.age)##23 print(mycls.name,mycls.phone)##sssss not attr

浙公网安备 33010602011771号