OK, now *this* is cool

Ype Kingma ykingma at accessforall.nl
Fri Dec 7 21:03:30 CET 2001


Bruce,

you wrote:
> 
> I think so, at least. Based on Tim's pointer about __subclasses__()
> in 2.2, I can automatically generate an instance of all of my
> subclasses.
> 
> class Flower(object):
>   def __repr__(self):
>     return self.__class__.__name__
> 
> class Gladiolus(Flower): pass
> class Runuculus(Flower): pass
> class Chrysanthemum(Flower): pass
> class Rose(Flower): pass
> class CrystalPassion(Rose): pass
> 
> print [Klass() for Klass in Flower.__subclasses__()]
> 
> Output:
> [Gladiolus, Runuculus, Chrysanthemum, Rose]
> 

print [Klass.__name__ for Klass in Flower.__subclasses__()]

would be easier to understand.
Somehow it doesn't feel right to define
__repr__ as only self.__class__.__name__
since repr() is normally used as the inverse of eval().

Did you consider defining __str__() instead?

Btw. thanks a lot for TIJ. 

Have fun,
Ype

P.S. Did they really make __subclasses__() a method?



More information about the Python-list mailing list