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! )
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With