Polymorphism means “many forms”. In programming, polymorphism allows the same function or method name to behave differently depending on the object that calls it.
In Python, polymorphism is mainly seen with:
Built-in functions that work with different types.
Class methods that are overridden in subclasses.
Duck typing — “If it walks like a duck and quacks like a duck, it’s a duck.”
Example 1: Built-in Polymorphism
Some Python functions work differently depending on the data type.
# len() works with multiple types
print(len("Hello")) # 5 → length of string
print(len([10, 20, 30])) # 3 → length of list
print(len({"a": 1, "b": 2})) # 2 → number of keys in dict
Same function name (len), different behavior depending on the data type.
Example 2: Polymorphism with Class Methods (Method Overriding)
When a child class overrides a method from its parent, both have the same name but different implementations.
class Bird:
def speak(self):
return "Some generic bird sound"
class Parrot(Bird):
def speak(self):
return "Squawk!"
class Crow(Bird):
def speak(self):
return "Caw!"
# Using polymorphism
for bird in [Parrot(), Crow(), Bird()]:
print(bird.speak())
Output:
Squawk!
Caw!
Some generic bird sound
Here, the same method name speak() behaves differently depending on the object’s class — that’s polymorphism.
Example 3: Polymorphism with a Common Interface
Multiple classes can implement the same method name, even if they’re unrelated.
class Cat:
def make_sound(self):
return "Meow"
class Dog:
def make_sound(self):
return "Woof"
class Cow:
def make_sound(self):
return "Moo"
# Using polymorphism
animals = [Cat(), Dog(), Cow()]
for animal in animals:
print(animal.make_sound())
Output:
Meow
Woof
Moo
Python doesn’t care what the object is, as long as it has a make_sound() method — this is called
duck typing:
“If it quacks like a duck, it’s a duck.”
Example 4: Polymorphism with Inheritance and super()
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
from math import pi
return pi * self.r ** 2
shapes = [Rectangle(5, 4), Circle(3)]
for shape in shapes:
print(f"Area: {shape.area():.2f}")
Output:
Area: 20.00
Area: 28.27
Both shapes use the same area() method — but behave differently.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Python, polymorphism is mainly seen with:
Example 1: Built-in Polymorphism
Some Python functions work differently depending on the data type.
Same function name (
len), different behavior depending on the data type.Example 2: Polymorphism with Class Methods (Method Overriding)
When a child class overrides a method from its parent, both have the same name but different implementations.
Output:
Here, the same method name
speak()behaves differently depending on the object’s class — that’s polymorphism.Example 3: Polymorphism with a Common Interface
Multiple classes can implement the same method name, even if they’re unrelated.
Output:
Python doesn’t care what the object is, as long as it has a
make_sound()method — this is called duck typing:Example 4: Polymorphism with Inheritance and
super()Output:
Both shapes use the same
area()method — but behave differently.Summary