Polymorphism in Python
Learn how the same method or operation can behave differently for different objects.
What is Polymorphism?
Polymorphism is an important concept of Object-Oriented Programming.
The word polymorphism means "many forms".
In Python, the same method, function or operation can behave differently depending on the object or data being used.
Example 📩🌍
Think about the word "sound".
A dog makes one type of sound, while a cat makes another type of sound.
The method name can remain the same:
sound(), but the behavior changes.
Polymorphism Diagram
Same Method
sound()
Dog
sound() → Bark
Cat
sound() → Meow
Cow
sound() → Moo
Different Behavior
Same method name, different output.
Simple Example
class Dog:
def sound(self):
print("Dog barks")
class Cat:
def sound(self):
print("Cat meows")
dog1 = Dog()
cat1 = Cat()
dog1.sound()
cat1.sound()
Output:
Dog barks
Cat meows
Both classes have the same method name
sound(), but the result is different.
Polymorphism through Method Overriding
When a child class provides its own version of a method that exists in the parent class, it is called method overriding.
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
dog1 = Dog()
cat1 = Cat()
dog1.sound()
cat1.sound()
Output:
Dog barks
Cat meows
Same Interface, Different Behavior
We can call the same method on different objects.
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output:
Dog barks
Cat meows
The loop does not need to know whether the object is a
Dog or a Cat. Each object
provides its own behavior for sound().
Polymorphism with Functions
A function can work with different objects as long as those objects provide the required method.
def make_sound(animal):
animal.sound()
make_sound(Dog())
make_sound(Cat())
Output:
Dog barks
Cat meows
Polymorphism with Built-in Functions
Python's built-in functions can also behave differently depending on the object or data type.
Example: len()
print(len("Python"))
print(len([10, 20, 30, 40]))
Output:
6
4
The same len() function works with both
a string and a list.
Operator Polymorphism
The same operator can behave differently depending on the data type.
print(10 + 20)
print("Hello " + "Python")
print([1, 2] + [3, 4])
Output:
30
Hello Python
[1, 2, 3, 4]
Here the + operator performs different
operations for numbers, strings and lists.
Duck Typing
Python follows a concept called duck typing.
The important thing is what an object can do, rather than exactly what type of object it is.
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
def make_sound(animal):
animal.sound()
make_sound(Dog())
make_sound(Cat())
Both objects have a sound() method,
so the same function can work with both.
Output
Bark
Meow
Different Forms of Polymorphism
Method Overriding
Child class changes parent method behavior.
Function Polymorphism
Same function works with different objects.
Operator Polymorphism
Same operator behaves differently for data types.
Benefits of Polymorphism
- Makes code flexible.
- Reduces repeated code.
- Makes programs easier to extend.
- Allows common interfaces.
- Makes code easier to maintain.
Common Mistakes
- Using different method names when a common behavior is expected.
-
Forgetting the
selfparameter. - Calling a method that does not exist in the object.
- Confusing polymorphism with inheritance.
Practice Programs
-
Create
DogandCatclasses with a commonsound()method. -
Create a
Vehicleclass and override a method in child classes. - Create a function that works with two different objects.
- Write examples of operator polymorphism.
- Create a small duck typing example.
Summary
Polymorphism means many forms. In Python, the same method, function or operator can behave differently depending on the object or data type. Method overriding, function polymorphism, operator polymorphism and duck typing are common ways to understand polymorphism in Python.