当前位置: 首页 > 图灵资讯 > 行业资讯> Python描述器中__getattribute__调用

Python描述器中__getattribute__调用

发布时间:2024-08-21 22:26:11

1、说明

在 Python 中 一切都是对象,所有对象都有默认的方法 __getattribute__(self, name)。

我们将使用这种方法 . 访问 obj 当属性自动调用时,为了防止递归调用,它总是从基类中实现 object 中获取 object.__getattribute__(self, name), 在大多数情况下,这种方法是默认的 self 的 __dict__ 字典中查找 name(搜索特殊方法除外)。

2、实例

描述器是由的 __getattribute__() 方法调用的一般逻辑如下:

def__getattribute__(self,key):
v=object.__getattribute__(self,key)
ifhasattr(v,'__get__'):
returnv.__get__(self)
returnv

重写 __getattribute__() 会阻止描述器的自动调用。

以上是Python描述器__getattribute__调用,希望对大家有所帮助。更多Python学习推荐:python教学

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09