Polymorphism in Python
Polymorphism is an important concept in Object-Oriented Programming that allows different objects to respond to the same action in their own way. It helps build flexible and scalable programs where behavior changes depending on the object that uses the method. This makes your code more dynamic and easier to extend.
What Is Polymorphism?
The word "polymorphism" means “many forms.” In Python, it allows one method name to behave differently based on the object calling it. You can write general code that works for many different object types.
Example: Same Method, Different Behavior
Each class may have a method with the same name, but each method performs a unique action. This lets Python automatically choose the correct version based on the object.
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
for animal in (Dog(), Cat()):
animal.sound()
The same method name sound() produces different outputs for different objects.
Polymorphism with Inheritance
When a child class overrides a parent method, Python uses the child version automatically. This lets multiple classes share a method name while keeping customized behavior.
class Vehicle:
def start(self):
print("Starting vehicle")
class Car(Vehicle):
def start(self):
print("Starting car engine")
class Bike(Vehicle):
def start(self):
print("Starting bike engine")
for v in (Car(), Bike()):
v.start()
Polymorphism with Functions
You can write a function that accepts different object types, as long as they share the same method name. This allows functions to work in a universal way without checking each type manually.
def operate(vehicle):
vehicle.start()
operate(Car())
operate(Bike())
This improves reusability because one function works with many object types.
Polymorphism with Built-In Functions
Some Python functions behave differently depending on the data type. This is another example of polymorphism built into the language.
print(len("Hello"))
print(len([10, 20, 30]))
The len() function knows how to handle strings, lists, and many other types in different ways.
Operator Overloading (Special Polymorphism)
Python allows operators like +, -, or * to behave differently depending on the operands.
This is also a form of polymorphism, because the same operator performs different actions.
print(10 + 20) # numbers
print("Hello " + "World") # strings
The operator adapts to the data types automatically.
Real-World Example
Polymorphism is widely used in systems that need interchangeable components.
For example, different payment methods may share a common method name like process() but implement their own behavior.
class Payment:
def process(self):
print("Processing payment")
class CardPayment(Payment):
def process(self):
print("Processing card payment")
class WalletPayment(Payment):
def process(self):
print("Processing digital wallet payment")
for p in (CardPayment(), WalletPayment()):
p.process()
📝 Practice Exercises
Exercise 1
Create two classes Plane and Helicopter with a method fly() and call both inside a loop.
Exercise 2
Create a parent class Account with a method calculate_interest().
Override it in two child classes.
Exercise 3
Write a function that accepts different object types and calls the same method on all of them.
Exercise 4
Show polymorphism using the len() function on at least three different data types.
✅ Practice Answers
Answer 1
class Plane:
def fly(self):
print("Plane is flying")
class Helicopter:
def fly(self):
print("Helicopter is flying")
for aircraft in (Plane(), Helicopter()):
aircraft.fly()
Answer 2
class Account:
def calculate_interest(self):
print("General interest")
class Savings(Account):
def calculate_interest(self):
print("Savings account interest")
class Business(Account):
def calculate_interest(self):
print("Business account interest")
for a in (Savings(), Business()):
a.calculate_interest()
Answer 3
def action(obj):
obj.run()
class Robot:
def run(self):
print("Robot moving")
class Athlete:
def run(self):
print("Athlete running")
action(Robot())
action(Athlete())
Answer 4
print(len("Skyline"))
print(len([1, 2, 3, 4]))
print(len((10, 20)))