Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8923

Re: Extracting property getters from dir() results

From Christian Heimes <lists@cheimes.de>
Subject Re: Extracting property getters from dir() results
Date 2011-07-06 11:35 +0200
References <433563dc-b5bb-42f1-9023-3ec799d25505@k13g2000vbv.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.694.1309944934.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extracting property getters from dir() results Gnarlodious <gnarlodious@gmail.com> - 2011-07-06 02:02 -0700
  Re: Extracting property getters from dir() results Christian Heimes <lists@cheimes.de> - 2011-07-06 11:35 +0200
    Re: Extracting property getters from dir() results Gnarlodious <gnarlodious@gmail.com> - 2011-07-06 07:51 -0700

csiph-web