listing all property variables of a class instance

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Jun 25 20:10:25 CEST 2007


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.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list