Suppose I have a class MyClass that has a property created with the @property decorator, like this:
class MyClass(object):
@property
def foo(self):
if whatever:
return True
else:
return False
Suppose I want to use the python inspect module to get the source code that defines the property. I know how to do this for methods (inspect.getsource) but I don't know how to do this for property objects. Anyone know how to do this?
Access the underlying getter function through the property's fget
attribute:
print(inspect.getsource(MyClass.foo.fget))
If it has a setter or deleter, you can access those through fset
and fdel
:
print(inspect.getsource(MyClass.foo.fset))
print(inspect.getsource(MyClass.foo.fdel))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.