What is polymorphism and duck typing in Python?
Reviewed by Gurusankar M.
Short Answer
Polymorphism means the same operation works on different types — the right method is chosen by the object. Python does this via duck typing: 'if it walks like a duck and quacks like a duck, it's a duck.' Code depends on behavior (methods an object supports), not on its declared class.
On this page
Concept Made Simple
Coffee Chat Question
“What is polymorphism and duck typing in Python?”
Remember It Faster
Mind Map Answer
- Polymorphism
- same call, type-specific behavior
- Duck typing
- care about methods, not the class
- Enabler
- override methods (e.g. __str__, speak)
- Formalize
- ABCs / typing.Protocol
Learn by Doing
Hands-on Keyboard
class Dog:
def speak(self): return "Woof"
class Cat:
def speak(self): return "Meow"
def announce(animal): # no type check
print(animal.speak()) # works if it has speak()
for a in (Dog(), Cat()):
announce(a)Woof Meow
Think Beyond the Expected
What If?
How does duck typing differ from Java-style interfaces?
Java checks types at compile time — an object must explicitly implement an interface. Python checks at runtime: if the object has the method you call, it works, regardless of its class or any declared interface. It's more flexible but shifts errors to runtime; typing.Protocol adds optional static checking.
Real World
Functions that accept 'any file-like object' or 'anything iterable' rely on duck typing — you can pass a real file, StringIO, or a custom class, and it just works as long as it has the needed methods.
Interviewer's Expectation
Keywords they're listening for:
- same op, different types
- duck typing = behavior over class
- runtime not compile-time
- method overriding
- Protocol/ABC to formalize
Common Mistakes
- Adding unnecessary isinstance() checks
- Assuming compile-time interface guarantees
- Ignoring the runtime-error risk of duck typing
Best Practices
- Program to behavior, not concrete types
- Use ABCs/Protocol where a contract must be explicit
- Handle the missing-method case gracefully
Follow-up Questions
- How do abstract base classes fit in?
- What is typing.Protocol (structural typing)?
- How does operator overloading relate (e.g. __add__)?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Polymorphism (Python) Interview question: "What is polymorphism and duck typing in Python?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair