python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
作者:junjie 时间:2023-04-29 08:57:32
分享一下刚遇到的一个小问题,我有一段类似于这样的python代码:
# coding: utf-8
class A(object):
@property
def _value(self):
# raise AttributeError("test")
return {"v": "This is a test."}
def __getattr__(self, key):
print "__getattr__:", key
return self._value[key]
if __name__ == '__main__':
a = A()
print a.v
运行后可以得到正确的结果
__getattr__: v
This is a test.
但是注意,如果把
# raise AttributeError("test")
这行的注释去掉的话,即在_value方法里面抛出AttributeError异常,事情就会变得有些奇怪。程序运行的时候并不会抛出异常,而是会进入一个无限递归:
File "attr_test.py", line 12, in __getattr__
return self._value[key]
File "attr_test.py", line 12, in __getattr__
return self._value[key]
RuntimeError: maximum recursion depth exceeded while calling a Python object
通过多方查找后发现是property装饰器的问题,property实际上是一个descriptor。在python doc中可以发现这样的文字:
object.__get__(self, instance, owner)
Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). owner is always the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner. This method should return the (computed) attribute value or raise an AttributeError exception.
这样当用户访问._value时,抛出了AttributeError从而调用了__getattr__方法去尝试获取。这样程序就变成了无限递归。
这个问题看上去不复杂,但是当你的_value方法是比较隐晦的抛出AttributeError的话,调试起来就会比较困难了。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中创建对象列表的实现示例
Python办公自动化之Excel介绍
![](https://img.aspxhome.com/file/2023/2/95602_0s.png)
快速掌握怎样选择准备安装的 MySQL版本
MySQL 数据库范式化设计理论总结
SHA256算法 asp源码
asp之自动闭合HTML/ubb标签函数+简单注释
详解Golang中Context的原理和使用技巧
Python+Opencv识别两张相似图片
![](https://img.aspxhome.com/file/2023/0/97070_0s.jpg)
Python functools模块学习总结
python同时给两个收件人发送邮件的方法
在Pycharm中设置默认自动换行的方法
好用的VSCode头部注释插件Fileheader Pro
windows server2016安装MySQL5.7.19解压缩版教程详解
mysql 超大数据/表管理技巧
把vgg-face.mat权重迁移到pytorch模型示例
利用Python的pandas数据处理包将宽表变成窄表
![](https://img.aspxhome.com/file/2023/1/112131_0s.png)
GoLang切片并发安全解决方案详解
详细介绍Ruby中的正则表达式
![](https://img.aspxhome.com/file/2023/8/101268_0s.jpg)
解决python selenium3启动不了firefox的问题
![](https://img.aspxhome.com/file/2023/2/105882_0s.jpg)