Bird
Raised Fist0
Pythonprogramming~30 mins

Best practices for multiple inheritance in Python - Mini Project: Build & Apply

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
Best practices for multiple inheritance
📖 Scenario: Imagine you are building a simple game where characters can have different abilities. Some characters can fly, some can swim, and some can do both. You want to use multiple inheritance to combine these abilities in a clean and safe way.
🎯 Goal: You will create classes for flying and swimming abilities, then create a character class that inherits from both. You will apply best practices to avoid common problems with multiple inheritance.
📋 What You'll Learn
Create two base classes: Flyer and Swimmer with simple methods
Create a class FlyingFish that inherits from both Flyer and Swimmer
Use super() to call parent methods properly
Print the abilities of the FlyingFish instance
💡 Why This Matters
🌍 Real World
Games, simulations, and GUI frameworks often use multiple inheritance to combine different features cleanly.
💼 Career
Understanding multiple inheritance and its best practices is important for writing maintainable and bug-free object-oriented code in Python.
Progress0 / 4 steps
1
Create base classes for abilities
Create a class called Flyer with a method move that returns the string "I can fly". Also create a class called Swimmer with a method move that returns the string "I can swim".
Python
Hint

Define two separate classes with a method named move that returns the exact strings.

2
Create FlyingFish class with multiple inheritance
Create a class called FlyingFish that inherits from Flyer and Swimmer. Inside it, define a method move that calls super().move() and returns its result.
Python
Hint

Use super() inside FlyingFish.move to call the parent method.

3
Add explicit method to show all abilities
Inside the FlyingFish class, add a method called all_moves that returns a list with the results of calling Flyer.move(self) and Swimmer.move(self).
Python
Hint

Call each parent class method directly with ClassName.method(self) to get all abilities.

4
Create instance and print abilities
Create an instance of FlyingFish called fish. Then print the result of fish.move() and fish.all_moves().
Python
Hint

Create the instance and print the outputs exactly as shown.

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()