iterate over class variables

Daniel Evers daniel.evers at rwth-aachen.de
Thu Nov 10 13:27:24 CET 2005


Hi!

You can iterate over the internal dictionary:

>>> class Test:
...     def __init__(self):
...             self.x = 5
...             self.y = 6
...             self.z = "Hallo"
... 
>>> x = Test()
>>> print x.__dict__
{'y': 6, 'x': 5, 'z': 'Hallo'}
>>> for key, value in x.__dict__.items():
...     print key
...     print value
... 
y
6
x
5
z
Hallo
>>> 

Consider using iteritems() instead of items() when you have a loop.

Hope that helps :)
Daniel



More information about the Python-list mailing list