-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipleinheritance_diamond_problem.py
72 lines (56 loc) · 1.61 KB
/
multipleinheritance_diamond_problem.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This module showcases how to solve the diamond problem and reliably call
the methods in the parent class(es) using the super() method.
"""
class BaseClass:
"""
The base class from which all other classes derive from.
"""
def call_me(self):
"""
Example method that is overridden in the child classes.
"""
print(f"Base Class Method executed")
class LeftSubClass(BaseClass):
"""
LeftSubClass inherits from BaseClass and overrides the call_me
method.
"""
def call_me(self):
"""
Overridden method from the BaseClass.
"""
print(f"LeftSubClass Method executed")
BaseClass.call_me(self)
class RightSubClass(BaseClass):
"""
RightSubClass inherits from BaseClass and overrides the call_me
method.
"""
def call_me(self):
"""
Overridden method from the BaseClass.
"""
print(f"RightSubClass Method executed")
BaseClass.call_me(self)
class ChildClass(LeftSubClass, RightSubClass):
"""
ChildClass inherits from LeftSubClass and RightSubClass. This class
overrides the call_me method found in both parents and the
BaseClass.
"""
def call_me(self):
"""
Overrides the method with the same name found in BaseClass,
LeftSubClass and RightSubClass.
"""
print(f"ChildClass Method executed")
LeftSubClass.call_me(self)
RightSubClass.call_me(self)
def main():
print("\nCall Me:")
print("--------")
test_object = ChildClass()
test_object.call_me()
if __name__ == '__main__':
main()