2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
Polymorphism in Python
Last Updated : 16 Dec, 2024
Polymorphism is a foundational concept in programming that allows
entities like functions, methods or operators to behave differently based
on the type of data they are handling. Derived from Greek, the term
literally means “many forms”.
Python’s dynamic typing and duck typing make it inherently
polymorphic. Functions, operators and even built-in objects like loops
exhibit polymorphic behavior.
Polymorphism in Built-in Functions
Python’s built-in functions exhibit polymorphism, adapting to various
data types.
Example:
1 print(len("Hello")) # String length
2 print(len([1, 2, 3])) # List length
3
4 print(max(1, 3, 2)) # Maximum of integers
5 print(max("a", "z", "m")) # Maximum in strings
Python determines behavior at runtime, enabling these functions to
work across diverse types without explicit type declarations.
Let’s explore polymorphism in detail:
[Link] 1/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
Table of Content
Polymorphism in Built-in Functions
Polymorphism in Functions
Polymorphism in Operators
Polymorphism in Object-Oriented Programming (OOP)
Types of Polymorphism
Inheritance Class Polymorphism
Polymorphism in Functions
Duck typing enables functions to work with any object regardless of its
type.
Example:
1 def add(a, b):
2 return a + b
3
4 print(add(3, 4)) # Integer addition
5 print(add("Hello, ", "World!")) # String
concatenation
6 print(add([1, 2], [3, 4])) # List concatenation
Polymorphism in Operators
Operator Overloading
In Python, operators like + behave polymorphically, performing addition,
concatenation or merging based on the data type.
Example:
1 print(5 + 10) # Integer addition
2 print("Hello " + "World!") # String concatenation
3 print([1, 2] + [3, 4]) # List concatenation
Python Course Python Tutorial Interview Questions Python Quiz Python Projects Practice Python
[Link] 2/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
Polymorphism in OOPs
In OOP, polymorphism allows methods in different classes to share the
same name but perform distinct tasks. This is achieved through
inheritance and interface design. Polymorphism complements other
OOP principles like inheritance (sharing behavior) and encapsulation
(hiding complexity) to create robust and modular applications.
Example:
1 class Shape:
2 def area(self):
3 return "Undefined"
4
5 class Rectangle(Shape):
6 def __init__(self, length, width):
7 [Link] = length
8 [Link] = width
9
10 def area(self):
11 return [Link] * [Link]
12
13 class Circle(Shape):
14 def __init__(self, radius):
15 [Link] = radius
16
17 def area(self):
18 return 3.14 * [Link] ** 2
19
20 shapes = [Rectangle(2, 3), Circle(5)]
21 for shape in shapes:
22 print(f"Area: {[Link]()}")
[Link] 3/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
Explanation:
The code showcases polymorphism using a parent class Shape and
child classes Rectangle and Circle.
Parent Class Shape: Contains a generic area method returning
“Undefined”, acting as a placeholder for derived classes to override.
Child Class Rectangle: Initializes length and width via the __init__
constructor. Overrides the area method to return the rectangle’s area
as length * width.
Child Class Circle: Initializes radius via the __init__ constructor.
Overrides the area method to return the circle’s area as 3.14 *
radius^2.
Polymorphic Behavior: A list of shape objects (Rectangle and Circle)
is created. A for loop iterates through the list, calling the area
method on each object. The method executed is determined by the
object’s type, showcasing polymorphism.
Types of Polymorphism
Compile-time Polymorphism
Found in statically typed languages like Java or C++, where the
behavior of a function or operator is resolved during the program’s
compilation phase.
Examples include method overloading and operator overloading,
where multiple functions or operators can share the same name but
perform different tasks based on the context.
In Python, which is dynamically typed, compile-time polymorphism is
not natively supported. Instead, Python uses techniques like dynamic
typing and duck typing to achieve similar flexibility.
Runtime Polymorphism
Occurs when the behavior of a method is determined at runtime
based on the type of the object.
In Python, this is achieved through method overriding: a child class
can redefine a method from its parent class to provide its own
[Link] 4/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
specific implementation.
Python’s dynamic nature allows it to excel at runtime polymorphism,
enabling flexible and adaptable code.
Example:
1 class Animal:
2 def sound(self):
3 return "Some generic sound"
4
5 class Dog(Animal):
6 def sound(self):
7 return "Bark"
8
9 class Cat(Animal):
10 def sound(self):
11 return "Meow"
12
13 # Polymorphic behavior
14 animals = [Dog(), Cat(), Animal()]
15 for animal in animals:
16 print([Link]()) # Calls the
overridden method based on the object type
Output
Bark
Meow
Some generic sound
Explanation: Here, the sound method behaves differently depending on
whether the object is a Dog, Cat or Animal and this decision happens at
runtime. This dynamic nature makes Python particularly powerful for
runtime polymorphism.
Inheritance Class Polymorphism
[Link] 5/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
Inheritance-based polymorphism occurs when a subclass overrides a
method from its parent class, providing a specific implementation. This
process of re-implementing a method in the child class is known as
Method Overriding.
Example:
1 class Animal:
2 def sound(self):
3 return "Some generic animal sound"
4
5 class Dog(Animal):
6 def sound(self):
7 return "Bark"
8
9 class Cat(Animal):
10 def sound(self):
11 return "Meow
Explanation:
Class Animal: Acts as the base (parent) class. Contains a method
sound that provides a default behavior, returning “Some generic
animal sound”. This serves as a generic representation of the sound
method for all animals.
Class Dog: Inherits from the Animal class (denoted by class
Dog(Animal)). Overrides the sound method to return “Woof Woof!”, a
behavior specific to dogs. This demonstrates method overriding,
where the subclass modifies the implementation of the parent class’s
method.
Class Cat: Inherits from the Animal class (denoted by class
Cat(Animal)). Overrides the sound method to return “Meow”, a
behavior specific to cats. Like Dog, this also demonstrates method
overriding.
Polymorphism in Python – FAQs
[Link] 6/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
What is method overriding in polymorphism?
Method overriding occurs when a subclass provides a specific
implementation of a method that is already defined in its
superclass. This allows the subclass to customize or completely
replace the behavior of the method inherited from the superclass.
Example:
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
dog = Dog()
print([Link]()) # Output: "Bark"
How to achieve polymorphism using class inheritance?
Polymorphism can be achieved using class inheritance by defining
a common interface in a base class and then providing specific
implementations in derived classes. This allows objects of
different classes to be treated as objects of a common superclass.
Example:
class Shape:
def draw(self):
raise NotImplementedError("Subclass must implement
abstract method")
class Circle(Shape):
def draw(self):
return "Drawing a circle"
class Square(Shape):
def draw(self):
return "Drawing a square"
shapes = [Circle(), Square()]
for shape in shapes:
print([Link]())
[Link] 7/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
# Output:
# Drawing a circle
# Drawing a square
Can polymorphism be achieved with functions in Python?
Yes, polymorphism can be achieved with functions in Python
through duck typing, where the method or function operates on
any object that supports the required method or attribute,
regardless of the object’s class.
Example:
def make_sound(animal):
return [Link]()
class Dog:
def sound(self):
return "Bark"
class Cat:
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(make_sound(animal))
# Output:
# Bark
# Meow
How does polymorphism help in writing flexible and
maintainable code?
Polymorphism allows you to write more flexible and maintainable
code by enabling you to use a single interface to represent
different underlying forms (data types). This means you can add
new classes with their own implementations without altering the
existing code, making it easier to extend and maintain.
Example:
[Link] 8/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
class Vehicle:
def move(self):
raise NotImplementedError("Subclass must implement
abstract method")
class Car(Vehicle):
def move(self):
return "Car is moving"
class Bike(Vehicle):
def move(self):
return "Bike is moving"
vehicles = [Car(), Bike()]
for vehicle in vehicles:
print([Link]())
# Output:
# Car is moving
# Bike is moving
What is the role of abstract base classes in polymorphism?
Abstract base classes (ABCs) define a common interface for a
group of subclasses. They cannot be instantiated themselves and
require subclasses to provide implementations for their abstract
methods. ABCs ensure that derived classes adhere to a specific
protocol, thus supporting polymorphism.
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print([Link]())
[Link] 9/14
2/2/25, 8:03 PM Polymorphism in Python - GeeksforGeeks
# Output:
# Bark
# Meow
Level up your coding with DSA Python in 90 days! Master key
algorithms, solve complex problems, and prepare for top tech
interviews. Join the Three 90 Challenge—complete 90% of the
course in 90 days and earn a 90% refund. Start your Python DSA
journey today!
Comment More info
Next Article
Placement Training Program Python | Method Overloading
Similar Reads
Ways of implementing Polymorphism in Python
In Python programming, Polymorphism is a concept of Object-Oriented
Programming in Python. It enables using a single interface with the inpu…
5 min read
Important differences between Python 2.x and Python 3.x with…
In this article, we will see some important differences between Python 2.x
and Python 3.x with the help of some examples. Differences between…
5 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like
object (e.g., normal files, StringIO objects, etc.). read() method has to be…
3 min read
Python | Add Logging to a Python Script
[Link] 10/14