Module 11 · Object-Oriented Programming — topic 20 of 25
Polymorphism
One interface, many forms: polymorphism through inheritance and overriding, duck typing, operator and built-in function polymorphism, and writing code that works with any type supporting an interface.
Learning objectives
- Write functions that work with many types through a shared interface
- Explain duck typing
- Recognise polymorphism in built-in functions and operators
Definition
Polymorphism means that the same operation or method call works on objects of different types, with each type supplying its own behaviour. In Python it comes from overriding and from duck typing: if an object has the needed method, it can be used.
Introduction
len works on strings, lists and dictionaries. + adds numbers and joins strings. A checkout function can charge a card, a UPI ID or a wallet. That is polymorphism: one call, many behaviours.
Why it matters
Polymorphism lets you add new types without changing the code that uses them. Instead of a growing if/elif chain on types, each object knows how to do its own job.
Detailed explanation
Inheritance-based polymorphism: subclasses override a method from a common parent, and calling code uses the parent's interface.
Duck typing: Python doesn't require a common parent. 'If it walks like a duck and quacks like a duck, it's a duck' — any object with a speak() method can be passed to code that calls speak().
Built-in polymorphism: len(), iter(), str() and the operators call special methods (__len__, __iter__, __str__, __add__), so they work on any type that defines them.
Code that checks types with long if isinstance(...) chains usually misses a chance for polymorphism: move the varying behaviour into the classes.
When a shared interface must be guaranteed, abstract base classes or typing.Protocol make the expectation explicit.
Syntax
- Same call, different behaviour per type.
- Rely on methods, not on type checks.
- Duck typing needs no common parent.
Code examples
Built-in polymorphism
str 5 list 3 dict 2 range 10 5 abcd [1, 2]
Duck typing
['Quack', 'Beep']
More examples
Payment methods behind one interface
UPI asha@bank: Rs 250 Card *4242: Rs 255.00 (incl. fee) Wallet: Rs 250, left Rs 50 Wallet: declined, balance Rs 100
Step-by-step explanation
- checkout knows nothing about UPI, cards or wallets; it only calls pay.
- Each class implements pay with its own rules: a card adds a fee, a wallet checks its balance.
- Adding a new method such as NetBanking means writing one class — checkout doesn't change.
Expected output
Running “Payment methods behind one interface” prints exactly:
Practical applications
- File-like objects: anything with read() can be passed to json.load.
- Payment gateways, storage backends and notification channels behind one interface.
Best practices
- Design around what objects can do, not what they are.
- Keep method names and signatures consistent across implementations.
- Use ABCs or Protocols to document required methods.
Common mistakes
Type-checking instead of polymorphism
Avoid
Prefer
Every new type would require editing the if/elif chain.
Important notes
- Python has no method overloading by parameter type; default arguments and functools.singledispatch cover those cases.
Key takeaways
- Polymorphism: one call, behaviour depends on the object.
- Comes from overriding and duck typing.
- Replaces type-checking chains with methods on the classes.
Try it yourself
- 1Add a NetBanking payment class and run it through checkout unchanged.
- 2Write a total_area(shapes) function that works with any object having an area() method.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
16 questions cover the “Object-Oriented Programming” module. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?
- ModerateWhat is printed?
- ModerateWhat is printed?