Changing ancestor classes at runtime

Chuck Esterbrook echuck at mindspring.com
Sun Mar 19 16:06:06 EST 2000


Thanks, this technique worked for me. The code I ended up using is this:

if not newBaseClass in aClass.__bases__:
        aClass.__bases__ = aClass.__bases__ + (newBaseClass,)

-Chuck


Darrell wrote:

> [Chuck Esterbrook]
> > Is there a way, at runtime, to make C also inherit from A.  The key here
> is _at runtime_.
> >
>
> class A:
>     def a(self):
>         print self.__class__
>
> class B:
>     def b(self):
>         print self.__class__
>
> class Top:
>     pass
>
> t=Top()
> l=list(Top.__bases__)
> l.append(B)
> l.append(A)
> Top.__bases__=tuple(l)
>
> t.b()
> t.a()
>
> ### Output
>
> __main__.Top
> __main__.Top
>
> --Darrell




More information about the Python-list mailing list