Python ProgrammingPython Programming

What does mro do?

mro stands for Method Resolution Order. It returns a list of types the class is derived from, in the order they are searched for methods.
class A(object):
    def dothis(self):
        print('From A class')
 
 
class B1(A):
    def dothis(self):
        print('From B1 class')
    pass
 
 
class B2(object):
    def dothis(self):
        print('From B2 class')
    pass
 
 
class B3(A):
    def dothis(self):
        print('From B3 class')
 
 
# Diamond inheritance
class D1(B1, B3):
    pass
 
 
class D2(B1, B2):
    pass
 
 
d1_instance = D1()
d1_instance.dothis()
print(D1.__mro__)
 
d2_instance = D2()
d2_instance.dothis()
print(D2.__mro__)
 
Sample output of above program.
From B1 class
(class '__main__.D1', class '__main__.B1', )
From B1 class
(class '__main__.D2', class '__main__.B1', , class '__main__.B2', class 'object')