[Tutor] How do I iterate through object attributes

Remco Gerlich scarblac@pino.selwerd.nl
Sun, 4 Feb 2001 23:35:17 +0100


On Sun, Feb 04, 2001 at 12:54:40PM -0900, Tim Johnson wrote:
> Newbie question here:
> I would like to be able to iterate through a function's attributes and return
> values for them: Code is below:
> def f():
> 	"f() doc string"
> 	z = 720
> 	x = 840
> f_dir =  dir(f)
> print "f() attributes:",f_dir
> f_attr = f_dir[0]
> print "first attribute:", f_attr
> print f.__doc__
> for attr in f_dir:
> 	print attr
> 	#print f.attr   #This Code generates AttributeError
> 
> To restate: How may I reference the value for one member of
> the attribute list of a function without the literal name of that attribute.

print getattr(f, attr)

-- 
Remco Gerlich