__getattr__

当属性查找不到时会报错
__getattr__和__getattribute__

___getattr__是在查找不到属性时调用,重写___getattr__就可以知道
__getattr__和__getattribute__
我们可以在__getattr__写自己的逻辑,以下是两个应用场景
1、当我们写错属性名字时,可以通过__getattr__修正过来
__getattr__和__getattribute__
2、例子中我们调用user.company,但是company在info里面,不能直接被访问,这样我们可以在__getattr__写逻辑让user.company直接被访问
__getattr__和__getattribute__

__getattribute__

当每次调用属性时,python会无条件进入__getattribute__中,不论属性存在与否,这就是与__getattr__的区别
__getattr__和__getattribute__

必须特别小心 getattribute() 方法,因为 Python 在查找类的方法名称时也将对其进行调用。
__getattr__和__getattribute__
__getattr__和__getattribute__

类中定义了一个总是引发 AttributeError 的 getattribute() 方法。没有属性或方法的查询会成功。

相关文章:

  • 2021-12-09
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2022-01-18
猜你喜欢
  • 2021-06-30
  • 2021-06-06
  • 2021-05-20
  • 2021-05-27
  • 2022-02-07
  • 2022-02-15
相关资源
相似解决方案