Polymorphism and Duck Typing
Write polymorphic code that works with any compatible object.
Introduction
Polymorphism lets different objects respond to the same interface. Duck typing means: if it behaves like a duck, it is a duck.
What is Polymorphism?
Polymorphism means 'many forms'. The same method name works differently on different objects. Python embraces this heavily.
class Cat:
def speak(self): return 'Meow'
class Dog:
def speak(self): return 'Woof'
for animal in [Cat(), Dog()]:
print(animal.speak())All lessons in this course
- Single Inheritance
- Method Overriding and super()
- Multiple Inheritance and MRO
- Polymorphism and Duck Typing