Hello,
I would like to be able to make a loop over a given class
attributes. For example, with the following class definition :
[color=blue][color=green][color=darkred]
>>> class x:[/color][/color][/color]
.... val1=1
.... val2=2
....[color=blue][color=green][color=darkred]
>>> print x.val1[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> print x.val2[/color][/color][/color]
2
I would like to be able to make a loop like this[color=blue][color=green][color=darkred]
>>> dir(x)[/color][/color][/color]
['__doc__', '__module__', 'val1', 'val2'][color=blue][color=green][color=darkred]
>>> for i in dir(x) : print x.i[/color][/color][/color]
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 @ouranos.ca
I would like to be able to make a loop over a given class
attributes. For example, with the following class definition :
[color=blue][color=green][color=darkred]
>>> class x:[/color][/color][/color]
.... val1=1
.... val2=2
....[color=blue][color=green][color=darkred]
>>> print x.val1[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> print x.val2[/color][/color][/color]
2
I would like to be able to make a loop like this[color=blue][color=green][color=darkred]
>>> dir(x)[/color][/color][/color]
['__doc__', '__module__', 'val1', 'val2'][color=blue][color=green][color=darkred]
>>> for i in dir(x) : print x.i[/color][/color][/color]
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 @ouranos.ca
Comment