liveBook · Manning

Multiple inheritance

As explained in The Quick Python Book, Fourth Edition

Multiple inheritance is an object-oriented language feature in which a class derives attributes and behavior from more than one parent class.

Multiple inheritance lets a Python class derive data and behavior from more than one parent class. The resulting class combines its own definitions with those inherited from its ancestors, and an instance can be used as an instance of each ancestor class. section 15.13

The basic rules are:

  • A class may list any number of parent classes, using the same syntax as single inheritance. section 15.13
  • When inherited classes have different method names, the subclass behaves like a synthesis of its definitions and its ancestors’ definitions. section 15.13
  • When several ancestors define the same method, Python searches base classes from left to right, completing one base class’s ancestor search before moving to the next. section 15.13
  • Multiple inheritance is also commonly used with mixin classes. section 15.15

A branched inheritance hierarchy

A class can combine several branches: B inherits from E and F, D inherits from G, and A inherits from B, C, and D. An instance of A can be used as an instance of A, B, C, D, E, F, or G. If several of those classes define the same method, the inheritance order determines which definition Python searches first. section 15.13

class E:
       . . .
class F:
       . . .
class G:
       . . .
class D(G):
       . . .
class C:
       . . .
class B(E, F):
       . . .
class A(B, C, D):
       . . .

Inspect the compact class declarations for the A–G multiple-inheritance hierarchy. section 15.13

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains declaration, inherited behavior, ancestor compatibility, and method-search ordering, without detailing the complete lookup algorithm.
Examples
Uses an A–G inheritance hierarchy to demonstrate multiple parents, indirect ancestors, instance compatibility, and conflicting methods.
What it assumes
Assumes general programming knowledge while introducing Python’s class and inheritance constructs without assuming a specific object-oriented language.
What you can do after
Evaluate Python inheritance hierarchies and predict which ancestor method a conflicting invocation searches first.
How it connects
Connects multiple inheritance with classes, inheritance hierarchies, method overriding, and mixin classes.

Also explained in

Compare how 4 books explain multiple inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.