looping over a class attribute
biner
biner.sebastien at ouranos.ca
Fri Mar 12 16:59:29 EST 2004
Hello,
I would like to be able to make a loop over a given class
attributes. For example, with the following class definition :
>>> class x:
... val1=1
... val2=2
...
>>> print x.val1
1
>>> print x.val2
2
I would like to be able to make a loop like this
>>> dir(x)
['__doc__', '__module__', 'val1', 'val2']
>>> for i in dir(x) : print x.i
and get an output like typing
print x.__doc__
print x__module__
print x.var1
print x.var2
instead of
Traceback (most recent call last):
File "<stdin>", line 2, in ?
AttributeError: class x has no attribute 'i'
Is there a way to do this? Thanks in advance from a clueless newbie to
the wonderful world of Python and OO programming.
Sebastien.
biner.sebastien at ouranos.ca
More information about the Python-list
mailing list