0Pricing
Python Academy · Lesson

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

  1. Single Inheritance
  2. Method Overriding and super()
  3. Multiple Inheritance and MRO
  4. Polymorphism and Duck Typing
← Back to Python Academy