0Pricing
Python Academy · Lesson

Multiple Inheritance and MRO

Understand multiple inheritance and Python's MRO (C3 linearization).

Multiple Inheritance and MRO is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Python supports multiple inheritance. The Method Resolution Order (MRO) defines which method is called when names clash.

Multiple Inheritance Syntax

class C(A, B): inherits from both A and B. C gets all methods from both.
class A:
    def hello(self): return 'A'
class B:
    def world(self): return 'B'
class C(A, B): pass
c = C()
print(c.hello(), c.world())

Method Resolution Order (MRO)

MRO defines lookup order. C.__mro__ shows the order: C, A, B, object. The first class with a matching method wins.
class A:
    def greet(self): return 'A'
class B:
    def greet(self): return 'B'
class C(A, B): pass
print(C().greet())  # A wins
print(C.__mro__)

C3 Linearization

Python uses C3 linearization to compute MRO. It guarantees: child before parent, left-to-right for multiple bases.
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print([c.__name__ for c in D.__mro__])

Diamond Inheritance

D(B,C) where B and C both inherit A is the diamond problem. C3 ensures A's __init__ is called only once.
class A:
    def __init__(self): print('A')
class B(A):
    def __init__(self): super().__init__(); print('B')
class C(A):
    def __init__(self): super().__init__(); print('C')
class D(B, C):
    def __init__(self): super().__init__(); print('D')
D()

Mixins

A mixin is a class designed for multiple inheritance. It adds methods without standing alone as a full class.
class JSONMixin:
    def to_json(self):
        import json
        return json.dumps(vars(self))
class User(JSONMixin):
    def __init__(self, name, age): self.name,self.age=name,age
print(User('Alice', 30).to_json())

Mixin Design Rules

Mixins should: not have __init__, not inherit from non-mixin classes, do one thing. Name them with Mixin suffix.
class LoggingMixin:
    def log(self, msg): print(f'[{self.__class__.__name__}] {msg}')
class Service(LoggingMixin):
    def run(self): self.log('running')
Service().run()

super() in Multiple Inheritance

super() calls the next class in the MRO, not necessarily the direct parent. Each class must call super() for cooperative behavior.
class Base:
    def action(self): return 'Base'
class A(Base):
    def action(self): return 'A->' + super().action()
class B(Base):
    def action(self): return 'B->' + super().action()
class C(A, B):
    def action(self): return 'C->' + super().action()
print(C().action())

type() for Dynamic Class Creation

type('NewClass', (Base,), {'method': fn}) creates a class dynamically. Useful for meta-programming.
def hello(self): return 'hello'
MyClass = type('MyClass', (object,), {'hello': hello})
print(MyClass().hello())

Checking the MRO

ClassName.__mro__ or inspect.getmro(ClassName) shows the resolution order as a tuple of classes.
class A: pass
class B(A): pass
class C(B): pass
import inspect
print([c.__name__ for c in inspect.getmro(C)])

When to Avoid Multiple Inheritance

Complex multiple inheritance hierarchies are hard to reason about. Prefer composition or simple mixins over deep hierarchies.
# Prefer:
class User:
    def __init__(self, name, logger):
        self.name = name
        self.logger = logger  # composition
print('composition preferred')

Quick Check

What algorithm does Python use to compute the Method Resolution Order?

Recap

Multiple inheritance: class C(A, B). MRO via C3 linearization determines method lookup. Use mixins for clean multiple inheritance. super() follows the MRO.

Keep Going

Excellent progress! Keep going to master the next concept.

Frequently asked questions

Is the “Multiple Inheritance and MRO” lesson free?

Yes — the full text of “Multiple Inheritance and MRO” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Multiple Inheritance and MRO”?

Understand multiple inheritance and Python's MRO (C3 linearization). You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multiple Inheritance and MRO” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Single Inheritance
  2. Method Overriding and super()
  3. Multiple Inheritance and MRO
  4. Polymorphism and Duck Typing
← Back to Python Academy