filmov
tv
Inheritance MRO method resolution order in OOPS for multilevel and multiple inheritance in Python
![preview_player](https://i.ytimg.com/vi/KXmM0RPOmdw/maxresdefault.jpg)
Показать описание
We are using inheritance where object can use methods of different classes. We may have common methods in different classes, then how Python will decide which order to follow the reach the desired method?
Here is an example of how Method resolution Order decide the method to use.
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y(x):# derived or child class
def m1(self):
print("I am y m1") # this will execute
y1=y()
y1.m1()
MRO with multiple inheritance
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y():
def m1(self):
print("I am y m1")
class z(x,y): # multiple inheritance
pass
z1=z() # object of z()
z1.m1() # method with class x() will be executed
Here is an example of how Method resolution Order decide the method to use.
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y(x):# derived or child class
def m1(self):
print("I am y m1") # this will execute
y1=y()
y1.m1()
MRO with multiple inheritance
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y():
def m1(self):
print("I am y m1")
class z(x,y): # multiple inheritance
pass
z1=z() # object of z()
z1.m1() # method with class x() will be executed