iterate over class variables
Yves Glodt
y.glodt at sitasoftware.lu
Thu Nov 10 07:23:33 EST 2005
Yves Glodt wrote:
> Hello list,
>
> I need to iterate over a class and get all her variable names and
> values, e.g. considering this example:
>
>
> class testclass:
> var1 = 'ab'
> var2 = 'cd'
> var3 = 'ef'
>
> test = testclass()
>
>
>
> Then I wanna do sonmething like this:
>
> for name,value in test:
> print name
> print value
>
> fails with of course with:
> "TypeError: iteration over non-sequence"
>
>
> How can I do that?
sorry for selfreplying, but I found a solution:
for key in dir(test):
if '__' not in key:
value = getattr(test,key)
print key, value
Does anything speak about this?
Is there a better-performing way to do this?
> regards,
> Yves
More information about the Python-list
mailing list