PY-101 ยท Object-oriented Python
Polymorphism
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Polymorphism means different classes can be used the same way because they share a method name, even though each does its own thing. A loop can call shape.area() on a circle and a square without knowing which is which. Python cares about what an object can do, not what it is ("duck typing").
- Call the same method on different classes
- Write code that works with any matching object
- Understand duck typing
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
class Card:def pay(self, amount):return f"Charged {amount} to card"class UPI:def pay(self, amount):return f"Requested {amount} via UPI"class Wallet:def __init__(self, balance):self.balance = balancedef pay(self, amount):self.balance -= amountreturn f"Paid {amount} from wallet, {self.balance} left"for method in [Card(), UPI(), Wallet(500)]:print(method.pay(120))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Charged 120 to card
Requested 120 via UPI
Paid 120 from wallet, 380 left ๐ฏ Add a Square class with side and an area() method, then print the total area of all shapes: Circle(1) area is 3.14, so the total is 3.14 + 16 = 19.14.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
class Square: def __init__(self, side): self.side = side; def area(self): return self.side ** 2
- The problemA checkout accepts cards, UPI and wallets. Each pays differently, but the checkout just calls method.pay(amount). Add a new payment type next year and the checkout code does not change.
- The Python idea
for thing in things:Polymorphism means different classes can be used the same way because they share a method name, even though each does its own thing. - In real lifeAI apps can switch between model providers because each client class offers the same generate() method; the rest of the app never changes.
Start with a question
A checkout accepts cards, UPI and wallets. Each pays differently, but the checkout just calls method.pay(amount). Add a new payment type next year and the checkout code does not change.
It is how plugins, payment providers, file formats and AI model clients can be swapped without rewriting the code that uses them.
Explain it like I'm 10
You say "speak!" to a dog, a cat and a parrot. Each answers in its own voice. You did not need three different commands.
The technical explanation
Each class defines a method with the same name and parameters, such as pay(self, amount).
Code that uses them just calls method.pay(amount) and does not check the type.
Duck typing: "if it walks like a duck and quacks like a duck", Python treats it as one. No shared parent is required.
Syntax
for thing in things:
thing.common_method() Real-world scenario
AI apps can switch between model providers because each client class offers the same generate() method; the rest of the app never changes.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Encapsulation โQuestions people ask
What is duck typing in Python?
Using an object based on what it can do rather than what class it is. If it has the method you call, it works.
What are abstract base classes for?
abc.ABC with @abstractmethod lets you require that every subclass implements certain methods, so mistakes are caught early.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is duck typing in Python?
Using an object based on what it can do rather than what class it is. If it has the method you call, it works.
beginner What are abstract base classes for?
abc.ABC with @abstractmethod lets you require that every subclass implements certain methods, so mistakes are caught early.
intermediate What is polymorphism?
Different classes answering the same method in their own way. Same call, different behaviour.
intermediate Does duck typing need a shared parent class?
No, just the same method. Python checks behaviour, not type.
Quick revision
Remember
- Call the same method on different classes
- Write code that works with any matching object
- Understand duck typing
- If an
if type(x) == ...chain keeps growing, that is a sign to give each class the same method and let polymorphism choose.
Syntax
for thing in things:
thing.common_method() Official documentation
- abc โ abstract base classes (docs.python.org)