在Python中,当你访问对象的一个属性(也就是写出类似obj.xxx的代码时),访问的优先级从高到低如下所示:
1.__getattribute__
这是优先级最高的函数,所有对属性的访问必先访问它。举个例子:
class Obj(object):
def __init__(self, weight, price):
self.weight = weight
self.price = price
def __getattribute__(self, name):
print name
return 2
obj = Obj(1,2)
print obj.__getattribute__
print obj.weight
得到的输出为:
__getattribute__ 2 weight 2
可以看到,哪怕是访问__getattribute__本身也要先经过__getattribute__函数