[Tutor] class attributes

Kent Johnson kent37 at tds.net
Wed Nov 16 20:19:00 CET 2005


Eric Walker wrote:
> Hello,
> If I have some like the following:
> 
> markersExp = ['big','boss','two','three']
> for mark in markersExp:
>      print y.mark
> 
> Now I have an list of class objects that are in an outerloop. y is how I 
> access it. The attributes names of the class objects match whats in 
> markersExp. Can I do that print statement in a way to allow me to print the 
> value of the class attribute?

Use getattr():
  print getattr(y, mark)

http://docs.python.org/lib/built-in-funcs.html#l2h-31

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list