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 fly1class Flyable: # fly returns the string "Flying"2 def fly(self):3 return "Flying"4 # A second class that provides the ability to swim5class Swimmable: # swim returns the string "Swimming"6 def swim(self):7 return "Swimming"8 # Duck inherits from both classes listed in the parentheses9class Duck(Flyable, Swimmable): # pass: Duck adds nothing of its own but gets both methods10 pass11 # Create a Duck instance12d = Duck()# Call one method from each parent; print joins them with a space13print(d.fly(), d.swim())Output
Flying Swimming
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