# class Foo:
# def __init__(self,x):
# self.x = x
# def __getattr__(self,item):
# print("__getattr__")
# # return self.__dict__[item]
# def printer(self):
# print("lsdajfl")
#
# f1 = Foo(10)
# print(f1.x)
# f1.abc # 访问不存在的属性,触发__getattr__
# f1.printer()
# class Foo1:
# def __init__(self,x):
# self.x = x
# def __getattribute__(self,item):
# print("__getattribute__")
# # 由上边的例子我们可以看出,不管属性存在不存在都会执行
# # __getattribute__
# f1 = Foo1(10)
# print(f1.x)
# print(f1.abc)
class Foo2:
def __init__(self,x):
self.x = x
def __getattr__(self,item):
print('执行__getattr__')
def __getattribute__(self,item):
print("执行__getattribute__")
def printer(self):
print(123)
# 当两个都存在的时候,只会执行__getattribute__
# 而不执行__getattr__
f2 = Foo2(20)
f2.x
f2.xxx
# f2.printer()
# 当我们修改了__getattribute__方法时,本来有的方法也无法执行了