__getattribute__

一、__getattr__

  • 不存在的属性访问,触发__getattr__
class Foo:
def __init__(self, x):
self.x = x def __getattr__(self, item):
print('执行的是我')
# return self.__dict__[item]
f1 = Foo(10)
print(f1.x)
f1.xxxxxx

10

执行的是我

二、__getattribute__

  • 查找属性无论是否存在,都会执行
class Foo:
def __init__(self, x):
self.x = x def __getattribute__(self, item):
print('不管是否存在,我都会执行') f1 = Foo(10)
f1.x
f1.xxxxxx

不管是否存在,我都会执行

不管是否存在,我都会执行

三、__getattr__与__getattribute__

  • 当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError
class Foo:
def __init__(self, x):
self.x = x def __getattr__(self, item):
print('执行的是我')
# return self.__dict__[item]
def __getattribute__(self, item):
print('不管是否存在,我都会执行')
raise AttributeError('哈哈') f1 = Foo(10)
f1.x

f1.xxxxxx

不管是否存在,我都会执行

执行的是我


不管是否存在,我都会执行

执行的是我

四、总结

  1. 当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,
  2. 只要__getattribute__在执行过程中抛出异常AttributeError,两个都会被执行

最新文章

  1. [LeetCode] Clone Graph 无向图的复制
  2. C# 生成条形码图片,效果不错
  3. 《图解TCP/IP》
  4. SQL中join的用法
  5. iOS 中使用md5加密
  6. JAXB--学习1
  7. 定时PING下IP地址,检测该服务器是否还活着。 smokeping
  8. Python tools used for file name devision
  9. 浏览器兼容汇总(css+js)
  10. 在linux系统中
  11. 21.命名空间别名限定符::和global全局名称空间限定符
  12. day12 python作业:员工信息表
  13. vue项目tips
  14. go语言变量
  15. js格式化数字
  16. java基础-反射(细节)
  17. Python开发——文件操作
  18. 迭代器 生成器 yield
  19. 微信小程序奇奇怪怪的语法
  20. 使用jQuery+huandlebars遍历展示对象中的数组

热门文章

  1. springmvc(@ResponseBody)无法跳转到对应的jsp页面
  2. 【Vue中的坑】Vue打包上传线上报Uncaught SyntaxError: Unexpected token <
  3. MySQL8.0安装caching_sha2_password问题
  4. ASP.NET Identity实现分布式Session,Docker+Nginx+Redis+ASP.NET CORE Identity
  5. https://www.jianshu.com/p/fc78dab5736f
  6. 干货分享:Essay写作收集论据的三个方法
  7. tf.summary可视化参数
  8. PyGame快速入门
  9. Mysql 3306 被 linux 防火墙拦截
  10. xdc如何设置输入延时