In Python a class can inherit from more than one class.

If a class inherits, it has the methods and variables from the parent classes.

In essence, it’s called multiple inheritance because a class can inherit from multiple classes. This is a concept from object orientated programming.

If you are totally new to (object orientated) programming, I recommend the course below.

Related course: Python Programming Courses & Exercises

Introduction

Inheritance is if a sub class gets all the methods and variables from a super class.

The inherited methods and variables can then be used in a newly created object from the sub class.

python inheritance

In the above image, class Student inherits from class Person.

So the method introduce() and variables firstname and lastname will be inside class Student.

If you create an object from the class Student, you can access both variables and methods from Student and Person.

In multiple inheritance, a class gets all the variables and methods from more than one parents.

Inheritance

To make a class inherit from a super class, you have to define the parenthesis in the class definition

class Sub(Super):
pass

Basic inheritance is done in this way in Python:

# Class
class A:
def a(self):
print('a')

# Class B inherits from Class A
class B(A):
def b(self):
print('b')

# Create object
obj = B()
B.a()
B.b()

Now let’s investigate multiple inheritance, inheriting from multiple classes.

Multiple inheritance

So what is multiple inheritance?

In multiple inheritance, a class inherits from two or more super classes.

It inherits the methods and variables from all super classes.

If you create an object, it has all methods and variables from the classes.

Lets see an example, where a class inherits from three classes

class Parent1:
pass

class Parent2:
pass

class Parent3:
pass

class Kid1(Parent1, Parent2, Parent3):
pass

These classes don’t have an implementation, but they show how to use multiple inheritance.

multiple inheritance example

To make a class inehrit from classes, just add them after the parenthesis.

class Kid1(Parent1):
class Kid1(Parent1, Parent2):
class Kid1(Parent1, Parent2, Parent3):
class Kid1(Parent1, Parent2, Parent3, Parent4):
class Kid1(Parent1, Parent2, Parent3, Parent4, Parent5):

Lets add some variables to the classes:

>>> class Parent1:
... x = 1
...
>>> class Parent2:
... y = 1
...
>>> class Parent3:
... z = 1
...
>>> class Kid1(Parent1, Parent2, Parent3):
... u = 2
...

Create a new object

>>> k = Kid1()

All variables are now available:

>>> k.x
1
>>> k.y
1
>>> k.z
1
>>> k.u
2
>>>

If you are a Python beginner, then I highly recommend this book.

Multiple Inheritance Example

Take a look at this multiple inheritance example:

>>> class Worker:
... title = ''
... pay = ''
...
>>> class TeamMember:
... project = ''
...
>>> class TeamLeader(TeamMember, Worker):
... experience = ''

You can create an object with every class.

Which variables will be available, depends on the class.

multiple inheritance example with two classes

The first two classes don’t inherit, so only those variables are available:

>>> bob = Worker()
>>> bob.
bob.pay bob.title

>>> jim = TeamMember()
>>> jim.project

If you create an object using class TeamLeader (multiple inheritance), it has many variables available.

>>> trevor = TeamLeader()
>>> trevor.
trevor.experience trevor.pay trevor.project trevor.title

Methods can be inherited too, the class TeamLeader below inherits all methods.

>>> class Worker:
... title = ''
... pay = ''
... def setTitle(self, title):
... self.title = title
... def setPay(self, pay):
... self.pay = pay
...
>>> class TeamMember:
... project = ''
... def setProject(self, project):
... self.project = project
...
>>> class TeamLeader(TeamMember, Worker):
... experience = ''
...
>>> jim = TeamLeader()
>>> jim.setTitle("Team Lead")
>>> jim.setPay(900000)
>>> jim.setProject("Python Zen")

Built-in inheritance methods in Python

Python has a few methods that help with inheritance.

The method isinstance() returns a boolean, telling you if an object is an instance of a class.

>>> obj = Boat()

>>> isinstance(obj, Car)
False
>>> isinstance(obj, Boat)
True

The method issubclass() tests if a class inherits from a class or not.

>>> class Vehicle:
... pass
...
>>> class Car(Vehicle):
... pass
...

>>> issubclass(Vehicle, Car)
False
>>> issubclass(Car, Vehicle)
True

If you are a Python beginner, then I highly recommend this book.

Multi-level inheritance

You may see multi-level inheritance, this is not multiple inheritance.

In multi-level there are several levels, which create an inheritance relationship.

This is similar to the relationship between grandpa, father and child.

>>> class Grandpa:
... x = 1
...
>>> class Father(Grandpa):
... y = 2
...
>>> class Kid(Father):
... z = 3
...
>>> k = Kid()
>>> f"{k.x} {k.y} {k.z}"
'1 2 3'

Here each class only inherits once, at most, but they inherit as a series.

multi-level inheritance

These two classes inherit once:

>>> class Father(Grandpa):
>>> class Kid(Father):

That’s why we call this multi-level inheritance.

The example below shows multi-level inheritance in a different context.

# Grandfather
>>> class Vehicle:
... engine = True

# Parent
>>> class AutoMobile(Vehicle):
... wheels = 4

# Children
>>> class Car(AutoMobile):
... seats = 4
...
>>> class Truck(AutoMobile):
... seats = 2
...

Multiple Inheritance vs Multi-Level inheritance

There are a few key differences between multiple inheritance and multi-level inheritance.

  • Multiple inheritance is when there are multiple super classes

  • Multiple inheritance can get complex, so not that widely used. Imagine six class inheritance, which variables and methods are available? You quickly lose the overview.

  • Multiple inheritance requires a two classs hierarchy: child and parents

  • Multi-level inheritance requires at least three levels of classes: grandfather, father and sons

If you are a Python beginner, then I highly recommend this book.

Download exercises