Is there a way to instantiate a subclass from a class constructor?

Paul Rubin phr-n2001 at nightsong.com
Sun Sep 23 01:44:52 EDT 2001


Say I have a class Polygon, with subclasses Rectangle (4 sides)
and Triangle (3 sides) which have some special operations.

I'd like to be able to call the Polygon constructor with a "sides"
argument and get a subclass instance if appropriate, e.g. I'd like to
call Polygon(sides=3) and get back a triangle, e.g. something like
   class Polygon:
     def __init__(sides=None):
        if sides==3: return Triangle(...)

but of course class initializers in Python don't return values.

Is wanting that bad OO practice in the first place?  Is there a way to
do it in Python?  A way to do it in other OO languages?  I'm not too
up on this type of thing.

The obvious workaround is to define a function that calls the appropriate
constructor, e.g.
  def newpoly(sides):
    if sides==3: return Triangle(...)
    ...
but this isn't so great, because it doesn't fit in with multiple base
classes:
   class RotatingMixin:
     def rotate(): ...    # make object rotate on the screen

   class RotatingPolygon(Polygon, RotatingMixin): pass

Ideally I'd like a definition like that to automatically result in
rotating triangles and rectangles as well as polygons.  I think CLOS
and/or Flavors can do things like that, but I've never used them.

Yet another workaround is to create a container object that has a
polygon as an instance variable:

   class PolygonWrapper:
     def __init__(self,sides):
        if sides==3: self.mypoly=Triangle(...)
        elif sides==4: self.mypoly=Rectangle(...)
        else: self.mypoly=Triangle

but now the wrapper class has to intercept all the operations that can
be done on any of the shapes, and pass them to the internal object.

Hmm, is something like
    def __getattr__(self,sym): return getattr(self.mypoly,sym)
in the PolygonWrapper class a reasonable solution?

I'm wondering how you OO wizards deal with this type of question.
It would seem to come up pretty often in OO designs. 

Regards from caveman C programming land,

--phr



More information about the Python-list mailing list