Bird
Raised Fist0
Pythonprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What is the main reason to use super() in multiple inheritance?

easy
A. To create a new instance of the child class
B. To call only the first parent class method
C. To avoid using any parent class methods
D. To ensure all parent classes are properly initialized

Solution

  1. Step 1: Understand the role of super() in multiple inheritance

    super() helps call the next method in the method resolution order (MRO), ensuring all parent classes get initialized properly.
  2. Step 2: Recognize why this is important

    Without super(), some parent classes might be skipped, causing incomplete initialization.
  3. Final Answer:

    To ensure all parent classes are properly initialized -> Option D
  4. Quick Check:

    Use super() to call all parents [OK]
Hint: Use super() to call all parents in order [OK]
Common Mistakes:
  • Calling only one parent class directly
  • Not using super() causing skipped initializations
  • Confusing super() with creating new instances
2.

Which of the following is the correct syntax to define a class Child inheriting from Parent1 and Parent2?

?
easy
A. class Child: Parent1, Parent2
B. class Child(Parent1, Parent2):
C. class Child inherits Parent1, Parent2:
D. class Child(Parent1 & Parent2):

Solution

  1. Step 1: Recall Python class inheritance syntax

    In Python, multiple inheritance is declared by listing parent classes inside parentheses separated by commas.
  2. Step 2: Match the correct syntax

    class Child(Parent1, Parent2): uses class Child(Parent1, Parent2):, which is the correct Python syntax.
  3. Final Answer:

    class Child(Parent1, Parent2): -> Option B
  4. Quick Check:

    Use parentheses with commas for multiple inheritance [OK]
Hint: Use parentheses with commas for multiple parents [OK]
Common Mistakes:
  • Using incorrect keywords like 'inherits'
  • Using '&' instead of commas
  • Placing parents outside parentheses
3.

What will be the output of the following code?

class A:
    def greet(self):
        print('Hello from A')

class B(A):
    def greet(self):
        print('Hello from B')
        super().greet()

class C(A):
    def greet(self):
        print('Hello from C')
        super().greet()

class D(B, C):
    def greet(self):
        print('Hello from D')
        super().greet()

d = D()
d.greet()
medium
A. Hello from D Hello from B Hello from C Hello from A
B. Hello from D Hello from C Hello from B Hello from A
C. Hello from D Hello from B Hello from A
D. Hello from D Hello from C Hello from A

Solution

  1. Step 1: Understand the method resolution order (MRO)

    For class D(B, C), the MRO is D > B > C > A. Calling super() follows this order.
  2. Step 2: Trace the calls

    d.greet() prints 'Hello from D', then calls B.greet() which prints 'Hello from B' and calls C.greet(). C.greet() prints 'Hello from C' and calls A.greet(), which prints 'Hello from A'.
  3. Final Answer:

    Hello from D Hello from B Hello from C Hello from A -> Option A
  4. Quick Check:

    MRO order = D, B, C, A [OK]
Hint: Follow MRO order when super() is called [OK]
Common Mistakes:
  • Ignoring MRO and calling parents in wrong order
  • Assuming super() calls only immediate parent
  • Missing one of the parent class prints
4.

Identify the error in the following code snippet using multiple inheritance:

class X:
    def __init__(self):
        print('X init')

class Y:
    def __init__(self):
        print('Y init')

class Z(X, Y):
    def __init__(self):
        X.__init__(self)
        Y.__init__(self)

z = Z()
medium
A. Class Z should inherit only from one parent
B. Missing call to super().__init__() in class Z
C. Directly calling parent __init__ methods can cause problems in complex hierarchies
D. No error, code runs fine

Solution

  1. Step 1: Analyze direct calls to parent __init__ methods

    Calling X.__init__(self) and Y.__init__(self) directly bypasses Python's MRO and can cause issues if the hierarchy grows complex.
  2. Step 2: Understand best practice

    Using super().__init__() respects MRO and avoids duplicate or missed calls.
  3. Final Answer:

    Directly calling parent __init__ methods can cause problems in complex hierarchies -> Option C
  4. Quick Check:

    Use super() to avoid init call issues [OK]
Hint: Avoid direct parent calls; use super() instead [OK]
Common Mistakes:
  • Thinking direct calls are always safe
  • Ignoring MRO and its importance
  • Believing multiple inheritance requires single parent only
5.

You want to create a class SmartPhone that inherits features from Camera and Phone. Both parents have an __init__ method. How should you design SmartPhone to properly initialize both parents following best practices?

hard
A. Define SmartPhone.__init__ and call super().__init__() only once, relying on parents to use super() too
B. Define SmartPhone.__init__ but leave it empty
C. Do not define __init__ in SmartPhone, parents will initialize automatically
D. Define SmartPhone.__init__ and call Camera.__init__(self) and Phone.__init__(self) directly

Solution

  1. Step 1: Understand multiple inheritance initialization

    Both Camera and Phone have __init__. To initialize both properly, each class should call super().__init__() so the MRO chain is followed.
  2. Step 2: Apply best practice in SmartPhone

    Define SmartPhone.__init__ and call super().__init__() once. This triggers the chain of __init__ calls in parents via MRO.
  3. Final Answer:

    Define SmartPhone.__init__ and call super().__init__() only once, relying on parents to use super() too -> Option A
  4. Quick Check:

    Use super() chain for clean multiple inheritance init [OK]
Hint: Call super().__init__() once; parents must do the same [OK]
Common Mistakes:
  • Calling parent __init__ methods directly
  • Not calling any __init__ in child
  • Assuming parents initialize automatically without super()