Python Class iteration howto !

Alex Martelli aleaxit at
Sat Sep 9 16:26:10 CEST 2000

"Sindh" <skodela at> wrote in message
news:8pd1k7$6t9$1 at
> 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)


More information about the Python-list mailing list