Extracting property getters from dir() results
Christian Heimes
lists at cheimes.de
Wed Jul 6 05:35:18 EDT 2011
Am 06.07.2011 11:02, schrieb Gnarlodious:
> Using introspection, is there a way to get a list of "property
> getters"?
>
> Does this:
>
> vars=property(getVars(), "Dump a string of variables and values")
>
> have some parsable feature that makes it different from other
> functions? Or would I need to use some naming scheme to parse them
> out?
dir() won't help you much here. The inspect module has several tools to
make inspection easier.
>>> import inspect
>>> class Example(object):
... @property
... def method(self):
... return 1
...
>>> inspect.getmembers(Example, inspect.isdatadescriptor)
[('__weakref__', <attribute '__weakref__' of 'Example' objects>),
('method', <property object at 0xec0520>)]
inspect.getmembers() with isdatadescriptor predicate works only on
classes, not on instances.
>>> inspect.getmembers(Example(), inspect.isdatadescriptor)
[]
Property instances have the attributes fget, fset and fdel that refer to
their getter, setter and delete method.
>>> for name, obj in inspect.getmembers(Example, inspect.isdatadescriptor):
... if isinstance(obj, property):
... print name, obj, obj.fget
...
method <property object at 0xec0520> <function method at 0xec1a28>
Christian
More information about the Python-list
mailing list