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 inheritance23class Flyer:4 def fly(self):5 print("Can fly")67class Swimmer:8 def swim(self):9 print("Can swim")1011class Duck(Flyer, Swimmer):12 pass1314d = 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.