galaxyan's answer suggesting composition is probably the best one. Multiple inheritance is often complicated to design and debug, and unless you know what you're doing, it can be difficult to get right. But if you really do want it, here's an answer explaining how you can make it work:
For multiple inheritance to work properly, the base classes will often need to cooperate with their children. Python's super
function makes this not too difficult to set up. You often will need a common base for the classes involved in the inheritance (to stop the inheritance chain):
class CommonBase:
def amethod(self):
print("CommonBase")
# don't call `super` here, we're the end of the inheritance chain
class Base1(CommonBase):
def amethod(self):
print("Base1")
super().amethod()
class Base2(CommonBase):
def amethod(self):
print("Base2")
super().amethod()
class Derived(Base1, Base2):
def amethod(self):
print("Derived")
super().amethod()
Now calling Derived().amethod()
will print Derived
, Base1
, Base2
, and finally CommonBase
. The trick is that super
passes each call on to the the next class in the MRO of self
, even if that's not the in the current class's inheritance hierarchy. So Base1.amethod
ends up calling Base2.amethod
via super
since they're being run on an instance of Derived
.
If you don't need any behavior in the common base class, its method body just be pass
. And of course, the Derived
class can just inherit the method without writing its own version and calling super
to get the rest.