How do I enumerate functions of a Python class marked with @property
?
class MyClass:
@property
def my_property():
pass
Something like this, but there is no lambda filter for inspect.isproperty
:
properties = inspect.getmembers(obj, inspect.isproperty)
Apparently, these are known as managed attributes.
This will do it:
inspect.getmembers(obj.__class__, lambda x: isinstance(x, property))
Here's how it works exactly (using IPython):
In [29]: class Foo(object):
....: @property
....: def foo(self): return 42
....:
In [30]: obj = Foo()
In [31]: inspect.getmembers(obj.__class__, lambda prop: isinstance(prop, property))
Out[31]: [('foo', <property at 0x106aec6d8>)]
This works because property
is a normal (new-style) class really; by marking something with @property
, you're just making an instance of property
. This also means that instances of properties (on a class) can be type-compared against property
using isinstance
.