FAQ
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?

-- Gnarlie

Search Discussions

  • Christian Heimes at Jul 6, 2011 at 9:35 am

    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
  • Gnarlodious at Jul 6, 2011 at 2:51 pm
    On Jul 6, 3:35?am, Christian Heimes wrote:

    Thank you! Exactly what I wanted.

    -- Gnarlie
    http://Gnarlodious.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 6, '11 at 9:02a
activeJul 6, '11 at 2:51p
posts3
users2
websitepython.org

2 users in discussion

Gnarlodious: 2 posts Christian Heimes: 1 post

People

Translate

site design / logo © 2022 Grokbase