[Tutor] class attributes
ewalker at micron.com
Wed Nov 16 23:02:43 CET 2005
ahh, you just blew smoke in my face. :)
I have a class with attributes. These attributes eventually have the value of
some lines in a file. I am now going back through my object list of that
class and assigning values to the attributes depending on another variable
that will change and be the same names like the class attribute names. I
want to use that change in name to be able to access the attribute and set
its value. Did I say that right????
On Wednesday 16 November 2005 02:53 pm, Alan Gauld wrote:
> > 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.
> Do you have to use string versions of the class names?
> Why not:
> class Mark: mark = 42
> class big(Mark): pass
> class boss(Mark): pass
> class two(Mark): pass
> class three(Mark): pass
> markersExp = [big,boss.two,three]
> for cls in markersExp: print cls.mark
> Classes are objects too!
> > Can I do that print statement in a way to allow me to print the
> > value of the class attribute?
> Or are we really talking about instances? In which case the usual solution
> is a dictionary.
> Alan g.
More information about the Tutor