Lesson 49 of 60
โฎPrev Nextโฏ

PY-101 ยท Object-oriented Python

Polymorphism

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

49of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Polymorphism, running
    1. class Card:
    2. def pay(self, amount):
    3. return f"Charged {amount} to card"
    4. class UPI:
    5. def pay(self, amount):
    6. return f"Requested {amount} via UPI"
    7. class Wallet:
    8. def __init__(self, balance):
    9. self.balance = balance
    10. def pay(self, amount):
    11. self.balance -= amount
    12. return f"Paid {amount} from wallet, {self.balance} left"
    13. for method in [Card(), UPI(), Wallet(500)]:
    14. print(method.pay(120))
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideafor 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.
    3. 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()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is polymorphism?
    2. 2. Does duck typing need a shared parent class?
    3. 3. Sign you could use polymorphism?
    4. 4. len("hi") and len([1, 2]) both work because?
    5. 5. Main benefit?

    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