Multiple Inheritance in Python

Level 4172 keystrokes

This demonstrates multiple inheritance where a class inherits from multiple parent classes. Duck inherits both fly and swim methods.

Python code

# A class that provides the ability to fly
1class Flyable:
# fly returns the string "Flying"
2 def fly(self):
3 return "Flying"
4
# A second class that provides the ability to swim
5class Swimmable:
# swim returns the string "Swimming"
6 def swim(self):
7 return "Swimming"
8
# Duck inherits from both classes listed in the parentheses
9class Duck(Flyable, Swimmable):
# pass: Duck adds nothing of its own but gets both methods
10 pass
11
# Create a Duck instance
12d = Duck()
# Call one method from each parent; print joins them with a space
13print(d.fly(), d.swim())

Output

Flying Swimming
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 7
  • )× 7
  • :× 5
  • "× 4
  • ,× 2
  • .× 2
  • =× 1

More Python examples at level 4

← Python typing practice