Python Polymorphism: Complete Guide with Examples
Master Python polymorphism - the ability to take many forms. Learn method overriding, operator overloading, duck typing with practical examples.
Method Overriding
Same method, different behavior
Operator Overloading
Custom operator behavior
Duck Typing
If it walks like a duck...
30+ Examples
Practical polymorphism patterns
What is Polymorphism?
Polymorphism means "many forms".
In Python, polymorphism allows the same method name, same operator, or same function to behave differently for different objects or data types.
Real-Life Example
A person can behave differently in different situations:
- Teacher in school
- Parent at home
- Customer in a shop
Similarly, Python methods and functions behave differently depending on the object.
Types of Polymorphism in Python
- Method Overriding
- Method Overloading
- Operator Overloading
- Duck Typing
1. Method Overriding
When a child class provides its own implementation of a parent class method.
Method Overriding Example
class Animal:
def sound(self):
print("Animal makes sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
d = Dog()
d.sound()
Output: Dog barks
Using super() Method
super() calls parent class methods.
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")
d = Dog()
d.sound()
Output:
Animal sound
Dog barks
2. Method Overloading
Python does not support traditional method overloading directly, but it can be achieved using default arguments.
Example of Method Overloading
class Math:
def add(self, a, b=0, c=0):
print(a + b + c)
m = Math()
m.add(10)
m.add(10, 20)
m.add(10, 20, 30)
Output:
10
30
60
3. Operator Overloading
Operators behave differently for different data types.
+adds numbers+concatenates strings
Built-in Operator Overloading
print(10 + 20)
print("Hello " + "Python")
Output:
30
Hello Python
Custom Operator Overloading
Using special methods like:
| Operator | Method |
|---|---|
| + | __add__() |
| - | __sub__() |
| * | __mul__() |
Example of Operator Overloading
class Box:
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.value + other.value
b1 = Box(10)
b2 = Box(20)
print(b1 + b2)
Output: 30
4. Duck Typing
Python follows: "If it looks like a duck and behaves like a duck, it is a duck."
Python focuses on behavior, not object type.
Duck Typing Example
class Dog:
def speak(self):
print("Dog barks")
class Cat:
def speak(self):
print("Cat meows")
def make_sound(animal):
animal.speak()
d = Dog()
c = Cat()
make_sound(d)
make_sound(c)
Output:
Dog barks
Cat meows
Polymorphism with Functions
len("Python")
len([1, 2, 3, 4])
len((10, 20))
len() works with different object types.
Polymorphism with Inheritance
class Shape:
def area(self):
print("Calculate area")
class Circle(Shape):
def area(self):
print("Area of circle")
class Rectangle(Shape):
def area(self):
print("Area of rectangle")
c = Circle()
r = Rectangle()
c.area()
r.area()
Advantages of Polymorphism
- Code reusability
- Flexibility
- Easy maintenance
- Better scalability
- Cleaner code structure
Real-Life Examples
| Example | Polymorphism |
|---|---|
| Person | Different roles |
| Mobile charger | Same port used differently |
| Remote control | Same buttons for different TVs |
Important Magic Methods
| Method | Purpose |
|---|---|
__add__() | Addition |
__sub__() | Subtraction |
__mul__() | Multiplication |
__str__() | String representation |
__len__() | Length |