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

 

posted @ 2019-11-28 19:01  howhy  阅读(75)  评论(0)    收藏  举报