Python __getattr__ __getattribute__

当调用对象属性时, Python会自动调用 getattribute, 当getattribute找不到属性时 会调用getattr
比如 a.dict 相当于执行了 a.getattribute(‘dict‘) 如果我们在重载getattribute中又调用dict的话,会无限递归
`class C(object):
def setattr(self, name, value):
print “setattr called:”, name, value
object.setattr(self, name, value)

def __getattr__(self, name):  
    print "__getattr__ called:", name  

def __getattribute__(self, name):  
    print "__getattribute__ called:",name  
    return object.__getattribute__(self, name)  

c = C()
c.x = “foo”`

猜你喜欢

转载自blog.csdn.net/dream_is_possible/article/details/69240469