\_\_getattribute\_\_
2024-10-08 18:44:11
__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
不管是否存在,我都会执行
执行的是我
不管是否存在,我都会执行
执行的是我
四、总结
- 当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,
- 只要__getattribute__在执行过程中抛出异常AttributeError,两个都会被执行
最新文章
- [LeetCode] Clone Graph 无向图的复制
- C# 生成条形码图片,效果不错
- 《图解TCP/IP》
- SQL中join的用法
- iOS 中使用md5加密
- JAXB--学习1
- 定时PING下IP地址,检测该服务器是否还活着。 smokeping
- Python tools used for file name devision
- 浏览器兼容汇总(css+js)
- 在linux系统中
- 21.命名空间别名限定符::和global全局名称空间限定符
- day12 python作业:员工信息表
- vue项目tips
- go语言变量
- js格式化数字
- java基础-反射(细节)
- Python开发——文件操作
- 迭代器 生成器 yield
- 微信小程序奇奇怪怪的语法
- 使用jQuery+huandlebars遍历展示对象中的数组
热门文章
- springmvc(@ResponseBody)无法跳转到对应的jsp页面
- 【Vue中的坑】Vue打包上传线上报Uncaught SyntaxError: Unexpected token <;
- MySQL8.0安装caching_sha2_password问题
- ASP.NET Identity实现分布式Session,Docker+Nginx+Redis+ASP.NET CORE Identity
- https://www.jianshu.com/p/fc78dab5736f
- 干货分享:Essay写作收集论据的三个方法
- tf.summary可视化参数
- PyGame快速入门
- Mysql 3306 被 linux 防火墙拦截
- xdc如何设置输入延时