Polymorphism
noun
say it: “pol-ee-MOR-fiz-um” /ˌpɒliˈmɔːrfɪzəm/
Different kinds of objects answering the same method call each in their own way, so one piece of code can work with all of them.
What is polymorphism?
Polymorphism means different objects can answer the same method call in their own way, so one piece of code can work with all of them.
class Card:
def pay(self, amount):
print(f"Charged ${amount} to card")
class Cash:
def pay(self, amount):
print(f"Took ${amount} in cash")
for method in [Card(), Cash()]:
method.pay(20)
In a sentence
- “Thanks to polymorphism, the checkout calls pay() without caring if it's a card or cash.”
- “Polymorphism let one loop draw circles, squares and triangles with the same draw() call.”
Level: Journeyman · More Python terms at this level
Updated