creating one of a family of classes

Roy Smith roy at popmail.med.nyu.edu
Thu Dec 2 20:11:39 EST 1999


I want to have a family of classes which are all subclasses of a common
ancestor.  For the sake of a simple example, let's say I have a class
polygon, with subclasses triangle, square, pentagon, and hexagon.

I want to be able to call the top-level creator, and have it return an
object of the appropriate subclass based on the argument I give it.  For
example:

p = polygon ([(1,2), (4,5), (0,4)])

would return an object of class triangle and

p = polygon ([(1,2), (4,5), (0,4), (3,4)])

would return an object of class square.  Is such a thing possible?




More information about the Python-list mailing list