Multiple Inheritance in Python

Duck(Flyer, Swimmer) gets methods from both. MRO decides who wins if names clash.

IntermediateObject-Oriented ProgramsExample 5 of 25
multiple-inheritance.py
1# Program to demonstrate multiple inheritance
2
3class Flyer:
4 def fly(self):
5 print("Can fly")
6
7class Swimmer:
8 def swim(self):
9 print("Can swim")
10
11class Duck(Flyer, Swimmer):
12 pass
13
14d = Duck()
15d.fly()
16d.swim()

Output

Can fly
Can swim

What's going on

Duck inherits behaviors from both Flyer and Swimmer via multiple inheritance.

After this program

If the idea is still fuzzy, read Day 11: Classes and Objects in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 object-oriented programs, or see every topic on Python exercises.