11

The following code doesn't work in Python 3.x, but it used to work with old-style classes:

class Extender:
    def extension(self):
        print("Some work...")

class Base:
    pass

Base.__bases__ += (Extender,)

Base().extension()

Question is simple: How can I add dynamically (at runtime) a super class to a class in Python 3.x?

But I'm ready the answer will be hard! )

2
  • 9
    Your bases are no longer belong to you? And the answer will hard?
    – Borealid
    Jul 7, 2010 at 8:37
  • I think this is not possible any more since all classes are new-style classes in Python 3, and the MRO would be ambiguous if Base had both object and Extender as base classes. Create a new class that inherits from Base and Extender instead. Extender might also be an ABC.
    – Philipp
    Jul 7, 2010 at 8:50

2 Answers 2

7

It appears that it is possible to dynamically change Base.__bases__ if Base.__base__ is not object. (By dynamically change, I mean in such a way that all pre-existing instances that inherit from Base also get dynamically changed. Otherwise see Mykola Kharechko's solution).

If Base.__base__ is some dummy class TopBase, then assignment to Base.__bases__ seems to work:

class Extender(object):
    def extension(self):
        print("Some work...")

class TopBase(object):
    pass

class Base(TopBase):
    pass

b=Base()
print(Base.__bases__)
# (<class '__main__.TopBase'>,)

Base.__bases__ += (Extender,)
print(Base.__bases__)
# (<class '__main__.TopBase'>, <class '__main__.Extender'>)
Base().extension()
# Some work...
b.extension()
# Some work...

Base.__bases__ = (Extender, TopBase) 
print(Base.__bases__)
# (<class '__main__.Extender'>, <class '__main__.TopBase'>)
Base().extension()
# Some work...
b.extension()
# Some work...

This was tested to work in Python 2 (for new- and old-style classes) and for Python 3. I have no idea why it works while this does not:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base.__bases__ = (Extender, object)
# TypeError: __bases__ assignment: 'Extender' deallocator differs from 'object'
2
  • The reason for the error of "cannot create a consistent.." is because you are trying to change Base.__base__ from the value (object,) to (object, Extender), which is the wrong way around according to mro-order. (Likewise you can't say class X(object, Extender) which errors with similarly). However Base.__bases__ += (Extender,) + Base.__bases__ will just throw 'E' deallocator differs from 'object', which is another error altogether, but still an error.
    – driax
    Aug 19, 2015 at 2:17
  • 4
    For future reference, there's a open CPython issue concerning the deallocator error: bugs.python.org/issue672115 (open since 2003 so don't hold your breath)
    – driax
    Aug 19, 2015 at 13:50
6

As for me it is impossible. But you can create new class dynamically:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base = type('Base', (Base, Extender, object), {})
Base().extension()
4
  • Thank you! Your solution works well! :) But I've updated it a little :$ It's enough to add a new base class only: Base = type('Base', (Extender,), {}) If you add 'Base' and 'object' classes again they will appear twice in the MRO and the interpreter will look for attributes slowly, i think. Jul 7, 2010 at 10:50
  • @DenisKolodin: The two Bases in the MRO are different classes: the new one and then the old one. Look at [id(cls) for cls in Base.__mro__] to see they are different.
    – unutbu
    Jul 7, 2010 at 12:49
  • I've tested it fully and have found one serious difference ((( Type will changed for new instances only, but with bases I can change attributes in exists instances that I need. This solution isn't similar! I still search ( Jul 8, 2010 at 7:26
  • @DenisKolodin: maybe try to use copy.deepcopy Jul 8, 2010 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.