-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_resolution_order.py
More file actions
93 lines (56 loc) · 1.46 KB
/
method_resolution_order.py
File metadata and controls
93 lines (56 loc) · 1.46 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# MRO => Method Resolution Order
# Used by python to search for the right method to use in classes having multi-inheritance
# Old MRO algorithm (DLR, Deep first, from left to right ): used in old python classes i.e not inheriting from object
# Old MRO algorithm (C3 Algorithm) : used in new python classes i.e inheriting from object
class A:
def whoami(self):
print("I am A")
class B:
def whoami(self):
print("I am B")
class C(A, B): # MRO of C will be C --> A ---> B
pass
class D(B, A): # MRO of D will be D --> B ---> A
pass
# class E(C, D): # MRO can't be decided because both parent class(C-> A,B D->B,A) have different MRO
# pass
c = C()
c.whoami()
d = D()
d.whoami()
# e = E()
# e.whoami()
class F(A):
def whoami(self):
print("I am in F")
class G(A):
def whoami(self):
print("I am in G")
class H(F, G):
def whoami(self):
print("I am G")
class I(G, F):
pass
# https://www.youtube.com/watch?v=cuonAMJjHow
h = H()
h.whoami()
i = I()
i.whoami()
print(I.__mro__)
print(H.__mro__)
# super with multiple Inheritance
class Base:
def basem(self):
print("Inside base")
class DerivedBase1(Base):
def basem(self):
super().basem()
class DerivedBase2(Base):
def basem(self):
print("Inside Derive base 2")
class Derive(DerivedBase1, DerivedBase2):
def basem(self):
super().basem()
print(Derive.__mro__)
derive = Derive()
derive.basem()