What is polymorphism and duck typing in Python?
⚡ 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.
☕Coffee Chat Question
Concept Made Simple
“What is polymorphism and duck typing in Python?”
🧠Mind Map Answer
Remember It Faster
⌨️Hands-on Keyboard
Learn by Doing
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
🔥What If?
Think Beyond the Expected
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:
⚠️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
- 1How do abstract base classes fit in?
- 2What is typing.Protocol (structural typing)?
- 3How does operator overloading relate (e.g. __add__)?
🧩Related Technologies
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?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.