Enumerate Python class properties (not functions)
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 : class Foo(object): ....: @property ....: def foo(self): return 42 ....: In : obj = Foo() In : inspect.getmembers(obj.__class__, lambda prop: isinstance(prop, property)) Out: [('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.