0
0
Pythonprogramming~20 mins

Diamond problem in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Diamond Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call in diamond inheritance
Consider the following Python code using diamond inheritance. What is the output when obj.greet() is called?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "Hello from B"

class C(A):
    def greet(self):
        return "Hello from C"

class D(B, C):
    pass

obj = D()
print(obj.greet())
A"Hello from C"
B"Hello from A"
C"Hello from B"
DTypeError
Attempts:
2 left
💡 Hint
Remember Python uses Method Resolution Order (MRO) to decide which method to call.
Predict Output
intermediate
2:00remaining
Understanding MRO list in diamond inheritance
What is the output of print(D.__mro__) for the following classes?
Python
class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

print(D.__mro__)
A(<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)
B(<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
C(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>)
D(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
Attempts:
2 left
💡 Hint
MRO follows the order of inheritance from left to right and then up the hierarchy.
Predict Output
advanced
2:00remaining
Output with super() in diamond inheritance
What will be printed when D().greet() is called in this code?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "B says " + super().greet()

class C(A):
    def greet(self):
        return "C says " + super().greet()

class D(B, C):
    def greet(self):
        return "D says " + super().greet()

print(D().greet())
A"D says B says C says Hello from A"
B"D says C says B says Hello from A"
C"D says Hello from A"
DAttributeError
Attempts:
2 left
💡 Hint
super() follows the MRO chain calling next method in line.
Predict Output
advanced
2:00remaining
Effect of changing inheritance order on output
What is the output of D().greet() if class D inherits as class D(C, B): instead of class D(B, C): in the previous example?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "B says " + super().greet()

class C(A):
    def greet(self):
        return "C says " + super().greet()

class D(C, B):
    def greet(self):
        return "D says " + super().greet()

print(D().greet())
A"D says C says B says Hello from A"
B"D says B says C says Hello from A"
C"D says Hello from A"
DTypeError
Attempts:
2 left
💡 Hint
Changing the order of inheritance changes the MRO and thus the order super() calls methods.
🧠 Conceptual
expert
2:00remaining
Why Python's diamond problem solution is safe
Why does Python's method resolution order (MRO) prevent the classic diamond problem issues seen in some other languages?
ABecause Python uses depth-first search to find methods, avoiding duplicates.
BBecause Python uses a linearization algorithm (C3) that ensures each class appears only once in the MRO, preventing duplicate calls.
CBecause Python requires explicit calls to parent methods, so no automatic method calls happen.
DBecause Python does not support multiple inheritance, so diamond problem cannot occur.
Attempts:
2 left
💡 Hint
Think about how Python orders classes in the MRO list.