Python Multiple Inheritance

Let’s look at multiple inheritance

  1. Notice

    With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Left:
    def label(self):
        return "left"
class Right:
    def label(self):
        return "right"
class Choice(Left, Right):
    pass
print(Choice().label())
Live state

Top-level scope

Left→Object 2
Right→Object 4
Choice→Object 5
Object 2 · typeclass LeftObject 4 · typeclass RightObject 5 · typeclass Choice
This step

With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

Picture multiple inheritance

With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

Image brief

Alt text
A diagram for Python Multiple Inheritance. Instructions: class Left: def label(self): return "left" class Right: def label(self): return "right" class Choice(Left, Right): pass print(Choice().label()). Displayed result: left. The final example bindings are Left --> object 2 (type); Right --> object 4 (type); Choice --> object 5 (type). object 2: class Left; object 4: class Right; object 5: class Choice
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Multiple Inheritance". Teaching focus: With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Left: def label(self): return "left" class Right: def label(self): return "right" class Choice(Left, Right): pass print(Choice().label()) | v DISPLAYED RESULT left FINAL BINDINGS Left --> object 2 (type) Right --> object 4 (type) Choice --> object 5 (type) object 2: class Left object 4: class Right object 5: class Choice Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Multiple Inheritance. Instructions: class Left: def label(self): return "left" class Right: def label(self): return "right" class Choice(Left, Right): pass print(Choice().label()). Displayed result: left. The final example bindings are Left --> object 2 (type); Right --> object 4 (type); Choice --> object 5 (type). object 2: class Left; object 4: class Right; object 5: class Choice

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Left:
    def label(self):
        return "left"
class Right:
    def label(self):
        return "right"
class Choice(Left, Right):
    pass
print(Choice().label())
Live state

Top-level scope

Left→Object 2
Right→Object 4
Choice→Object 5
Object 2 · typeclass LeftObject 4 · typeclass RightObject 5 · typeclass Choice
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
left

What do you expect this program to print?

What does this example display?

class Left:
    def label(self):
        return "left"
class Right:
    def label(self):
        return "right"
class Choice(Left, Right):
    pass
print(Choice().label())
What does this example display?
Choose one answer. You can try again.

Answer and explanation

left

Follow the printed values in order. The output is: left

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

More examples and reference

In Python, a class can inherit from more than one parent class. This is called Multiple Inheritance.

Don't worry if this sounds confusing. Let's break it down with very simple examples.

What is Inheritance?

Inheritance means one class (called the child) can use code from another class (called the parent).

With multiple inheritance, a child class can use code from two or more parent classes.

Simple Example of Multiple Inheritance

# First Parent Class
class Father:
    def skills(self):
        print("Father: Cooking, Driving")

# Second Parent Class
class Mother:
    def hobbies(self):
        print("Mother: Painting, Gardening")

# Child class inherits from both Father and Mother
class Child(Father, Mother):
    pass

# Create an object of Child
c = Child()
c.skills()
c.hobbies()
Father: Cooking, Driving
Mother: Painting, Gardening

Explanation:

  • The Child class inherits from both Father and Mother.
  • That means the child object c can use functions from both parent classes.
  • c.skills() comes from the Father class.
  • c.hobbies() comes from the Mother class.

What If Both Parents Have the Same Function Name?

This is where it gets interesting. Python follows something called MRO (Method Resolution Order).

Let's see an example:

class Father:
    def talk(self):
        print("Father: I talk about sports.")

class Mother:
    def talk(self):
        print("Mother: I talk about arts.")

class Child(Father, Mother):
    pass

c = Child()
c.talk()
Father: I talk about sports.

Explanation:

  • Both Father and Mother have a method called talk().
  • But the Child class inherits from Father first.
  • So, Python runs the method from Father and skips the one from Mother.

How to Check MRO (Method Resolution Order)?

You can check which class Python will look at first using this:

print(Child.__mro__)
(<class '__main__.Child'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>)

Explanation:

This tells you the order in which Python will check for methods:

  1. First it looks in Child.
  2. If not found, it looks in Father.
  3. Then in Mother.
  4. And finally in object, which is the default base class in Python.

Conclusion

Multiple inheritance lets a class use features from more than one parent. Just remember:

  • Child class can access methods from all parents.
  • If two parents have the same method, Python will use the one from the parent listed first.
  • You can check the method order using __mro__.

What to remember

Connect what you noticed in the example with the idea below.

Remember

With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.

Watch out for this idea

Python chooses a parent method randomly.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Encapsulation →