内置函数__getattribute__

 1 class Foo:
 2     x = 2
 3 
 4     def __init__(self, name):
 5         self.name = name
 6 
 7     def __getattr__(self, item):
 8         print('没有该属性')
 9 
10     def __getattribute__(self, item):
11         print('打印getattribute')
12         raise AttributeError('jajajajaj')  # 抛出异常,继续运行__getattr__
13 
14 
15 f1 = Foo('小白')
16 print(f1.y)
17 print(f1.x)
18 输出:
19 打印getattribute
20 没有该属性
21 None
22 打印getattribute
23 没有该属性
24 None

__getattribute__    如果定义了此方法,就不会正常取到本该取到的值,因为这个方法会实现,不管能不能找到某个属性都会执行此方法,找到了有的属性,会执行此方法,没有找到属性,也会执行此方法,那么__geattr__ 就不会被执行,只有当设置一个raise方法,就会继续执行__getattr__ 方法。

原文地址:https://www.cnblogs.com/ch2020/p/12444320.html