Changing ancestor classes at runtime

Darrell darrell at dorb.com
Sun Mar 19 11:32:18 EST 2000


[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