dynamic inheritance

Michele Simionato michele.simionato at gmail.com
Wed Jun 21 04:07:02 EDT 2006


alf wrote:
> I did not think about any particular problem, just thought it would be
> cool to abstract out the base class. In fact you can do that in C++ (to
> some extend) using templates and parameterizing the base class.

Python is ways cooler than C++. This is a sensible use case where you
may
want to change the base class at runtime:

>>> class Base(object):
...     pass

>>> class BasePlusDebugMethods(Base):
...     pass
...

>>> class C(Base):
...     pass

>>> C.__bases__ = (BasePlusDebugMethods,)

>>> C.mro()
[<class '__main__.C'>,
 <class '__main__.BasePlusDebugMethods'>,
 <class '__main__.Base'>,
 <type 'object'>]

(i.e. in a running program with a problem you can add debug methods and
possibily
even fix the problem without restarting the program).

       Michele Simionato




More information about the Python-list mailing list