Discover instance variables

Lawrence Oluyede raims at dot.com
Mon Jul 16 23:33:40 CEST 2007


Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> The list keeps only the types explicitely enumerated. Other types are
> excluded, as well as names starting with "_". Another criterion would be
> `not attr.startswith('_') and not callable(getattr(obj,attr))`

Why not:

In [1]: class Foo(object):
   ...:     a = 3
   ...:     
   ...:     

In [2]: import inspect

In [4]: inspect.getmembers(Foo())
Out[4]: 
[('__class__', <class '__main__.Foo'>),
 ('__delattr__', <method-wrapper '__delattr__' of Foo object at
0x12bf350>),
 ('__dict__', {}),
 ('__doc__', None),
 ('__getattribute__',
  <method-wrapper '__getattribute__' of Foo object at 0x12bf350>),
 ('__hash__', <method-wrapper '__hash__' of Foo object at 0x12bf350>),
 ('__init__', <method-wrapper '__init__' of Foo object at 0x12bf350>),
 ('__module__', '__main__'),
 ('__new__', <built-in method __new__ of type object at 0x307860>),
 ('__reduce__', <built-in method __reduce__ of Foo object at
0x12bf350>),
 ('__reduce_ex__', <built-in method __reduce_ex__ of Foo object at
0x12bf350>),
 ('__repr__', <method-wrapper '__repr__' of Foo object at 0x12bf350>),
 ('__setattr__', <method-wrapper '__setattr__' of Foo object at
0x12bf350>),
 ('__str__', <method-wrapper '__str__' of Foo object at 0x12bf350>),
 ('__weakref__', None),
 ('a', 3)]

and then filter out __xyz__ methods?


-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list