[英]get source code for method with @property decorator python
假設我有一個MyClass類,該類具有使用@property裝飾器創建的屬性,如下所示:
class MyClass(object):
@property
def foo(self):
if whatever:
return True
else:
return False
假設我想使用python inspect模塊來獲取定義屬性的源代碼。 我知道如何為方法(inspect.getsource)執行此操作,但我不知道如何為屬性對象執行此操作。 有人知道怎么做嗎?
通過屬性的fget
屬性訪問基礎的getter函數:
print(inspect.getsource(MyClass.foo.fget))
如果它具有設置器或刪除器,則可以通過fset
和fdel
訪問它們:
print(inspect.getsource(MyClass.foo.fset))
print(inspect.getsource(MyClass.foo.fdel))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.