[Tutor] class attributes

Alan Gauld alan.gauld at freenet.co.uk
Wed Nov 16 22:53:21 CET 2005


> 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 mailing list