0
0
Pythonprogramming~20 mins

Best practices for multiple inheritance in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method resolution order (MRO) in multiple inheritance
What is the output of this Python code showing the method resolution order (MRO) for classes with multiple inheritance?
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__.C'>, <class '__main__.A'>, <class 'object'>)
D(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>)
Attempts:
2 left
💡 Hint
Remember Python uses C3 linearization for MRO in multiple inheritance.
Predict Output
intermediate
2:00remaining
Output of method calls in diamond inheritance
What is the output of this code demonstrating method calls in diamond multiple inheritance?
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())
AHello from D
BHello from C
CHello from A
DHello from B
Attempts:
2 left
💡 Hint
Check which parent class appears first in the inheritance list of class D.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple inheritance with conflicting methods
What error does this code raise when trying to create an instance of class D?
Python
class A:
    def action(self):
        return 'Action from A'

class B:
    def action(self):
        return 'Action from B'

class D(A, B):
    pass

obj = D()
print(obj.action())
ANo error, output: Action from A
BNo error, output: Action from B
CAttributeError: 'D' object has no attribute 'action'
DTypeError: Cannot create a consistent method resolution order (MRO) for bases A, B
Attempts:
2 left
💡 Hint
Check if the classes A and B have a common ancestor and how Python resolves method calls.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in multiple inheritance?
Which of the following class definitions will cause a syntax error?
A
class D(A B):
    pass
B
class D(A, B):
    pass
C
class D(A, B):
    def method(self):
        pass
D
class D(A, B):
    def __init__(self):
        super().__init__()
Attempts:
2 left
💡 Hint
Check the syntax for listing multiple base classes in Python.
🚀 Application
expert
3:00remaining
Determine the final output with super() in multiple inheritance
What is the output of this code that uses super() in a multiple inheritance scenario?
Python
class A:
    def process(self):
        return 'A'

class B(A):
    def process(self):
        return super().process() + 'B'

class C(A):
    def process(self):
        return super().process() + 'C'

class D(B, C):
    def process(self):
        return super().process() + 'D'

obj = D()
print(obj.process())
AABCD
BACBD
CABDC
DACDB
Attempts:
2 left
💡 Hint
Trace the calls following Python's MRO and how super() delegates method calls.