先给出结论,从高到底:
__getattribute__()方法
实例属性
继承的实例属性(从父类继承的__init__()方法)
类属性
父类属性
__getattr__()方法
class Foo:
x = 'Foo attr'
def __init__(self):
self.x = 'father instance attr'
class Foo_sub(Foo):
# pass
x = 'Foo_sub attr'
def __init__(self):
self.x = 'Foo_sub instance attr'
def __getattr__(self, item):
print('last __getattr__ method')
def __getattribute__(self, item):
print('highest property method')
f = Foo_sub()
print(f.x)
print(f.y)
优先级从高往低注释掉观察运行的结果(结果就不贴了