-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmultiple-inheritance-2.py
55 lines (34 loc) · 1.15 KB
/
multiple-inheritance-2.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# multiple-inheritance-2.py
# Python supports multiple inheritance
# It uses a depth-first order when searching for methods.
# This search pattern is call MRO (Method Resolution Order)
# This is a second example, which shows the lookup of 'dothis()'.
# Both A and C contains 'dothis()'. Let's trace how the lookup happens.
# As per the MRO output using depth-first search,
# it starts in class D, then B, A, and lastly C.
# Here we're looking for 'dothis()' which is defined in class `C`.
# The lookup goes from D -> B -> A -> C.
# Since class `A` doesn't have `dothis()`, the lookup goes back to class `C`
# and finds it there.
class A(object):
def dothat(self):
print("Doing this in A")
class B(A):
pass
class C(object):
def dothis(self):
print("\nDoing this in C")
class D(B, C):
"""Multiple Inheritance,
D inheriting from both B and C"""
pass
d_instance = D()
d_instance.dothis()
print("\nPrint the Method Resolution Order")
print(D.mro())
'''
O/P-
Doing this in C
Print the Method Resolution Order
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
'''