Middle+
What is MRO (Method Resolution Order)?
sobes.tech AI
Answer from AI
MRO (Method Resolution Order) is the order in which Python searches for methods and attributes in the class hierarchy during inheritance, especially in multiple inheritance. Python uses the C3 linearization algorithm, which guarantees that the search occurs in a consistent and predictable manner, avoiding ambiguities.
Example:
class A:
def method(self):
print("A")
class B(A):
def method(self):
print("B")
class C(A):
def method(self):
print("C")
class D(B, C):
pass
obj = D()
obj.method() # Will print "B"
print(D.mro()) # Shows the method search order
Here, the method is called from class B, as the MRO for D is [D, B, C, A, object].