10.1 Inheriting from Multiple Classes
Multiple inheritance in Python lets a class inherit attributes and methods from more than one parent class. This gives you more flexibility and lets you reuse code, but can also lead to complex hierarchies and potential conflicts.
Having the ability to list five parents for your class significantly expands your options and makes coding super convenient. It’s really simple to do — just list the parent classes separated by a comma:
class Base1:
def method1(self):
print("Method1 from Base1")
class Base2:
def method2(self):
print("Method2 from Base2")
class Derived(Base1, Base2):
pass
obj = Derived()
obj.method1()
obj.method2()
Everything works as expected — sweet!
However, multiple inheritance has its quirks that you must consider when using it. Let's explore how it works and ways to sidestep issues with multiple inheritance.
10.2 Calling a Method Present in Multiple Base Classes
A class can inherit attributes and methods from several parent classes listed in parentheses after the class name. And these attributes and methods might have the same name:
class Base1:
def method(self):
print("Method from Base1")
class Base2:
def method(self):
print("Method from Base2")
class Derived(Base1, Base2):
pass
obj = Derived()
obj.method() # which method will be called?
In this example, the Derived class inherits from Base1 and Base2. When method() is called, Python will choose the method from the first listed class — Base1.
But it’s not that obvious, right? And if someone tweaks the base class code, the whole app logic might take a hit, and you wouldn’t even know something's off. Just slightly different methods might start getting called :)
10.3 Using super() with Multiple Inheritance
Another neat thing is using super() for the base class in multiple inheritance.
Example:
class Base1:
def method(self):
print("Method from Base1")
super().method()
class Base2:
def method(self):
print("Method from Base2")
super().method()
class Derived(Base1, Base2):
def method(self):
print("Method from Derived")
super().method()
obj = Derived()
obj.method()
So, what’s gonna be printed?
Method from Derived
Method from Base1
Or
Method from Derived
Method from Base2
Got a surprise for you — here’s what actually gets printed:
Method from Derived
Method from Base1
Method from Base2
The code super().method() calls the method() from each base class! This is one of those peculiarities I was talking about regarding multiple inheritance.
10.4 Diamond Inheritance
And finally, the classic diamond inheritance problem. It’s easier to show with an example than to describe:
As code, it might look something like this:
class A:
def method(self):
print("Method from A")
class B(A):
def method(self):
print("Method from B")
super().method()
class C(A):
def method(self):
print("Method from C")
super().method()
class D(B, C):
def method(self):
print("Method from D")
super().method()
obj = D()
obj.method()
The output will be:
Method from D
Method from B
Method from C
Method from A
To get a handle on multiple inheritance, you need to understand the order in which Python looks for fields and methods in parent classes. You’ll learn about that in the next lecture.
GO TO FULL VERSION