Dynamic subclassing ?

Karlo Lozovina _karlo_ at _mosor.net
Sat May 12 12:57:06 EDT 2007


manatlan wrote:

> I can't find the trick, but i'm pretty sure it's possible in an easy
> way.

It's somewhat easy, boot looks ugly to me. Maybe someone has a more 
elegant solution:

In [6]: import new

In [13]: class Button:
    ....:     def buttonFunc(self):
    ....:         pass

In [14]: class ExtensionClass:
    ....:     def extendedMethod(self):
    ....:         pass

In [15]: hybrid = new.instance(Button, 
Button.__dict__.update(ExtensionClass.__dict__))

In [17]: dir(hybrid)
Out[17]: ['__doc__', '__module__', 'buttonFunc', 'extendedMethod']

Seems to do what you want it to do.

HTH,
Karlo.












More information about the Python-list mailing list