Python Class iteration howto !

Piet van Oostrum piet at
Sat Sep 9 13:26:50 CEST 2000

>>>>> Sindh <skodela at> (S) writes:

S> Hi all
S> Suppose I have a class xyz with attributed name,age,sex,address etc. How
S> do I iterate the values.

S> eg:

S> class xyz:
S> 	name=''
S> 	age=0
S> 	sex='U'
S> 	address='U'

S> def iter(a):
S> 	for n in dir(a):
S> 		print a.n

S> but the above code says no attribute called a.n. How do I tell python to
S> substitute n for a attrib from list.

class xyz:
    def __init__(self):'Piet'

def iter(a):
    for n in dir(a):
        print a.__dict__[n]
# or    print getattr(a,n)


Piet van Oostrum <piet at>
Private email: P.van.Oostrum at

More information about the Python-list mailing list