'What does "mro()" do?
What does mro() do?
Example from django.utils.functional:
for t in type(res).mro(): # <----- this
if t in self.__dispatch:
return self.__dispatch[t][funcname](res, *args, **kw)
Solution 1:[1]
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.
mro() and __mro__ work only on new style classes. In Python 3, they work without any issues. In Python 2, however, those classes need to inherit from object.
Solution 2:[2]
This would perhaps show the order of resolution.
class A(object):
def dothis(self):
print('I am from A class')
class B(A):
pass
class C(object):
def dothis(self):
print('I am from C class')
class D(B, C):
pass
d_instance = D()
d_instance.dothis()
print(D.mro())
The output would be:
I am from A class
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
The rule is depth-first, which in this case would mean D, B, A, C.
Python normally uses a depth-first order when searching inheriting classes, but when two classes inherit from the same class, Python removes the first mention of that class from the MRO.
Solution 3:[3]
Order of resolution will be different in diamond inheritance.
class A(object):
def dothis(self):
print('I am from A class')
class B1(A):
def dothis(self):
print('I am from B1 class')
# pass
class B2(object):
def dothis(self):
print('I am from B2 class')
# pass
class B3(A):
def dothis(self):
print('I am from B3 class')
# Diamond inheritance
class D1(B1, B3):
pass
class D2(B1, B2):
pass
d1_instance = D1()
d1_instance.dothis()
# I am from B1 class
print(D1.__mro__)
# (<class '__main__.D1'>, <class '__main__.B1'>, <class '__main__.B3'>, <class '__main__.A'>, <class 'object'>)
d2_instance = D2()
d2_instance.dothis()
# I am from B1 class
print(D2.__mro__)
# (<class '__main__.D2'>, <class '__main__.B1'>, <class '__main__.A'>, <class '__main__.B2'>, <class 'object'>)
Solution 4:[4]
For @stryker 's example, the C3 algorithm is:
L[D(B,C)] = D + merge(BAo, Co, BC)
= D + B + merge(Ao, Co, C)
= DB + A + merge(o, Co, C)
= DBA + merge(Co, C)
= DBACo
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Waygood |
| Solution 2 | |
| Solution 3 | Girish Gupta |
| Solution 4 |
