0
0
Pythonprogramming~20 mins

Method Resolution Order (MRO) in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MRO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of MRO with multiple inheritance
What is the output of this Python code when calling obj.method()?
Python
class A:
    def method(self):
        return "A"

class B(A):
    def method(self):
        return "B"

class C(A):
    def method(self):
        return "C"

class D(B, C):
    pass

obj = D()
print(obj.method())
AD
BC
CB
DA
Attempts:
2 left
💡 Hint
Remember Python uses C3 linearization for MRO and checks parents left to right.
Predict Output
intermediate
2:00remaining
MRO list of a complex class
What is the output of print(E.__mro__) for the following code?
Python
class X:
    pass

class Y(X):
    pass

class Z(X):
    pass

class W(Y, Z):
    pass

class E(W, Z):
    pass

print(E.__mro__)
A(E, W, Z, Y, X, object)
B(E, W, Y, Z, X, object)
C(E, W, Y, Z, object, X)
D(E, W, Y, X, Z, object)
Attempts:
2 left
💡 Hint
Check the order of base classes and how Python merges MROs.
🔧 Debug
advanced
2:00remaining
Identify the cause of MRO conflict error
Why does this code raise a TypeError: Cannot create a consistent method resolution order (MRO)?
Python
class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

class E(C, B):
    pass

class F(D, E):
    pass
ABecause classes D and E inherit from B and C in different orders causing MRO conflict in F
BBecause class A is missing a method required by F
CBecause class F does not define its own __init__ method
DBecause classes B and C have no common base class
Attempts:
2 left
💡 Hint
Look at the order of base classes in D and E and how F inherits from both.
Predict Output
advanced
2:00remaining
Output of super() in diamond inheritance
What is the output of this code?
Python
class Root:
    def greet(self):
        return "Hello from Root"

class Left(Root):
    def greet(self):
        return "Left says " + super().greet()

class Right(Root):
    def greet(self):
        return "Right says " + super().greet()

class Child(Left, Right):
    def greet(self):
        return "Child says " + super().greet()

obj = Child()
print(obj.greet())
AChild says Left says Right says Hello from Root
BChild says Right says Left says Hello from Root
CChild says Left says Hello from Root
DChild says Hello from Root
Attempts:
2 left
💡 Hint
super() follows the MRO chain, calling next method in order.
🧠 Conceptual
expert
2:00remaining
Understanding MRO with metaclasses
Given the following code, what is the output of print(type(C).__mro__)?
Python
class Meta(type):
    pass

class A(metaclass=Meta):
    pass

class B(A):
    pass

class C(B):
    pass

print(type(C).__mro__)
A(<class '__main__.Meta'>, <class 'object'>, <class 'type'>)
B(<class 'type'>, <class 'object'>)
C(<class 'type'>, <class '__main__.Meta'>, <class 'object'>)
D(<class '__main__.Meta'>, <class 'type'>, <class 'object'>)
Attempts:
2 left
💡 Hint
Remember that the type of a class is its metaclass, and metaclasses inherit from type.