Challenge - 5 Problems
MRO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
Remember Python uses C3 linearization for MRO and checks parents left to right.
✗ Incorrect
Class D inherits from B and C. B overrides method() to return 'B', C returns 'C'. Python looks for method in D, then B, then C, then A. So it finds B's method first.
❓ Predict Output
intermediate2: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__)
Attempts:
2 left
💡 Hint
Check the order of base classes and how Python merges MROs.
✗ Incorrect
Python merges MROs of parents using C3 linearization. E inherits from W and Z. W inherits from Y and Z. The final MRO is (E, W, Y, Z, X, object).
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Look at the order of base classes in D and E and how F inherits from both.
✗ Incorrect
D inherits from B then C, E inherits from C then B. When F inherits from D and E, Python cannot find a consistent order that respects both, causing the MRO conflict error.
❓ Predict Output
advanced2: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())
Attempts:
2 left
💡 Hint
super() follows the MRO chain, calling next method in order.
✗ Incorrect
Child's MRO is [Child, Left, Right, Root, object]. Each greet calls super().greet(), so calls chain Left -> Right -> Root.
🧠 Conceptual
expert2: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__)
Attempts:
2 left
💡 Hint
Remember that the type of a class is its metaclass, and metaclasses inherit from type.
✗ Incorrect
C is a class whose metaclass is Meta (inherited from A). The type of C is Meta. Meta inherits from type, so MRO is (Meta, type, object).