Selecting a different superclass

Chris Rebert clp at rebertia.com
Wed Dec 17 15:33:23 EST 2008


On Wed, Dec 17, 2008 at 6:41 AM, psaffrey at googlemail.com
<psaffrey at googlemail.com> wrote:
> This might be a pure OO question, but I'm doing it in Python so I'll
> ask here.
>
<snip>
>
> The problem is that IDPointSet and MicroArrayPointSet will need to
> inherit from PointSet or TraceablePointSet based on whether I'm
> handling traceable points or not. Can I select a superclass
> conditionally like this in Python? Am I trying to do something really
> evil here?

You're doing something really dynamic; don't think I'd call it
inherently evil though.
All you have to do is use a variable in the inheritance syntax, it's
really quite simple:

superclass = TraceablePointSet if tracing else PointSet

class IDPointSet(superclass):
    #body code here

#use same trick for MicroArrayPointSet

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list