Friday, December 3, 2010

change class inheritance

>>> class A(): pass
...
>>> class B(A): pass
...
>>> b = B()
>>> isinstance(b, A)
True
>>> B.__bases__
(<class __main__.A at 0x7f552b3f36b0>,)
>>> B.__bases__ = ()
>>> isinstance(b, A)
False

The super-classes of a class are available under the __bases__ attribute.  These can be modified to dynamically change the class inheritance tree.

No comments:

Post a Comment