python的描述符(descriptor)、裝飾器(property)造成的一個無限遞歸問題分享
分享一下剛遇到的一個小問題,我有一段類似于這樣的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
運(yùn)行后可以得到正確的結(jié)果
__getattr__: v
This is a test.
但是注意,如果把
# raise AttributeError("test")
這行的注釋去掉的話,即在_value方法里面拋出AttributeError異常,事情就會變得有些奇怪。程序運(yùn)行的時候并不會拋出異常,而是會進(jìn)入一個無限遞歸:
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
通過多方查找后發(fā)現(xiàn)是property裝飾器的問題,property實際上是一個descriptor。在python doc中可以發(fā)現(xiàn)這樣的文字:
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.
這樣當(dāng)用戶訪問._value時,拋出了AttributeError從而調(diào)用了__getattr__方法去嘗試獲取。這樣程序就變成了無限遞歸。
這個問題看上去不復(fù)雜,但是當(dāng)你的_value方法是比較隱晦的拋出AttributeError的話,調(diào)試起來就會比較困難了。
相關(guān)文章
利用python實現(xiàn)平穩(wěn)時間序列的建模方式
這篇文章主要介紹了利用python實現(xiàn)平穩(wěn)時間序列的建模方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Pytest+Request+Allure+Jenkins實現(xiàn)接口自動化
這篇文章介紹了Pytest+Request+Allure+Jenkins實現(xiàn)接口自動化的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
最強(qiáng)Python可視化繪圖庫Plotly詳解用法
數(shù)據(jù)分析離不開數(shù)據(jù)可視化。Plotly 是一款用來做數(shù)據(jù)分析和可視化的在線平臺,功能非常強(qiáng)大,可以在線繪制很多圖形比如條形圖、散點(diǎn)圖、餅圖、直方圖等等2021-11-11

