views:

426

answers:

1

This solves my issue. No subclassing needed.

class DrvCrystalfontzProxy(Text):
    @classmethod
    def get(cls, model, visitor, obj=None, config=None):
        if model not in Models.keys():
            error("Unknown Crystalfontz model %s" % model)
            return
        model = Models[model]
        if model.protocol == 1:
            MyClass = Protocol1
        elif model.protocol == 2:
            MyClass = Protocol2
        elif model.protocol == 3:
            MyClass = Protocol3
        return MyClass(model, visitor, obj, config)
+2  A: 

I'm not sure I'm clear on your desired use here, but it is possible to subclass dynamically. You can use the type object to dynamically construct a class given a name, tuple of base classes and dict of methods / class attributes, eg:

>>> MySub = type("MySub", (DrvCrystalfontz, some_other_class), 
         {'some_extra method' : lamba self: do_something() })

MySub is now a subclass of DrvCrystalfontz and some_other_class, inherits their methods, and adds a new one ("some_extra_method").

Brian
I certainly wasn't aware of this use of type. Thanks for the pointer.
Scott