-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPolymorphism.py
More file actions
32 lines (23 loc) · 781 Bytes
/
Copy pathPolymorphism.py
File metadata and controls
32 lines (23 loc) · 781 Bytes
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
# Always latest version of the method is executed
# Same name method in same class
class Test:
def display(self):
print('Display')
def display(self,x):
print('Display ',x)
t = Test()
#t.display() # Error, because display(self,x) is the latest definition and that expects on argument to be paased
t.display(10) # Display 10
# Same name method in parent and child class
class A:
def display(self):
print('Display of A')
class B(A):
def display(self,x):
print('Display Of B with vlaue ',x)
b = B()
#b.display() # Error
b.display(10) #Display Of B with vlaue 10
# Every class has a method mro which specifies the Method Resolution Order
print(B.mro())
# [<class '__main__.B'>, <class '__main__.A'>, <class 'object'>]