Python Class iteration howto !

Alex Martelli aleaxit at yahoo.com
Sat Sep 9 10:26:10 EDT 2000


"Sindh" <skodela at my-deja.com> wrote in message
news:8pd1k7$6t9$1 at nnrp1.deja.com...
    [snip]
> class xyz:
> name=''
> age=0
> sex='U'
> address='U'
>
> def iter(a):
> for n in dir(a):
> print a.n
>
> but the above code says no attribute called a.n. How do I tell python to
> substitute n for a attrib from list.

For example, you can use the built-in function getattr:
    print getattr(a,n)


Alex






More information about the Python-list mailing list