listing all property variables of a class instance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jun 25 20:52:58 CEST 2007


En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch  
<bj_666 at gmx.net> escribió:

> In <mailman.35.1182792042.22759.python-list at python.org>, Jay Loden wrote:
>> Neil Cerutti wrote:
>>>> Is there a way to write a method that would list automatically
>>>> all the variables defined as a property (say by printing their
>>>> docstring and/ or their value), and only those variables?
>>>
>>> This is off the cuff. There's likely a better way.
>>>
>>> for k, v in MyClass.__dict__.iteritems():
>>>   if isinstance(v, property):
>>>     print k, v.__doc__
>>>
>>
>> The only way I could get this to work was to change the way the
>> properties were defined/initalized:
>
> That's because you iterate over the instance's `__dict__` and not over  
> the
> *class* `__dict__` like Neil does.

I would iterate over dir(MyClass) instead - only because I prefer to hide  
such implementation details.

-- 
Gabriel Genellina




More information about the Python-list mailing list