OK, now *this* is cool

Bruce Eckel Bruce at EckelObjects.com
Fri Dec 7 11:13:49 EST 2001


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]

Note that you'd have to do a recursive call to get all the classes
in the tree, as __subclasses__() only goes one level down.

Most current information can be found at:
http://www.mindview.net/Etc/notes.html
===================
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java 2e" & "Thinking
in C++ 2e"
Please subscribe to my free newsletter -- just send any email to:
join-eckel-oo-programming at earth.lyris.net
My schedule can be found at:
http://www.mindview.net/Calendar
===================






More information about the Python-list mailing list