Journey to Master Python (WIP)

ageekymonk
2,763 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Introspection

Multiple Inheritance

Multiple Inheritance with two non overlapping parents

Multiple Inheritance is easy if we inherit from two parent class, each without any overlapping methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Father:
print("In Father")
def funcA(self):
print('In funcA')
class Mother:
print("In Mother")
def funcB(self):
print('In funcB')
class Son(Father, Mother):
print("I am son")
x = Son()
x.funcA()
x.funcB()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Multiple Inheritance with overlapping methods

If there are overlapping methods, the method of first parent class will be called. In the below example, since we added the class Father as the first argument in class Son(Father, Mother), the method in class Father will be called. If it had been class Son(Mother, Father) then method in class Mother will be called.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Father:
print("In Father")
def funcA(self):
print('In Father funcA')
class Mother:
print("In Mother")
def funcA(self):
print('In Mother funcA')
class Son(Father, Mother):
print("I am son")
x = Son()
x.funcA()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Multiple Inheritance with Diamond Structure

To solve this python computes mro, method resolution order for a class. This can be found by __mro__. This gives a list of parent classes linearized. So python checks for next in line based on the __mro__.

super follows the mro as well. It does not call the parent of a class. It calls next in line based mro.

The algorithm used for computing the mro is C3 Linearization Algorithm. More details on the algorithm in mro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Adam:
pass
class Eve:
pass
class PaternalGrandFather(Adam, Eve):
def funcA(self):
print('In Paternal Grandfather funcA')
class PaternalGrandMother(Adam, Eve):
pass
class MaternalGrandFather(Adam, Eve):
def funcA(self):
print('In Maternal GrandFather funcA')
class MaternalGrandMother(Adam, Eve):
pass
class Father(PaternalGrandFather, PaternalGrandMother):
pass
class Mother(MaternalGrandFather, MaternalGrandMother):
def funcA(self):
print('In Mother funcA')
class Son(Father, Mother):
pass
print(Son.__mro__)
x = Son()
x.funcA()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content