The Wayback Machine - https://web.archive.org/web/20261119084234/https://www.geeksforgeeks.org/python/polymorphism-in-python/

Polymorphism in Python

Last Updated : 13 Nov, 2026

Polymorphism means "many forms" and allows the same method, function or operator to behave differently depending on the object or data it works with. This flexibility helps create more reusable, maintainable and scalable code. Real-Life Example:

  • In an online payment system, different payment methods such as Credit Card, UPI, Wallet and NetBanking all use the same processPayment() method.
  • The method name remains the same, but each payment type performs its own specific payment process.

Types of Polymorphism

Polymorphism refers to the ability of the same method or operation to behave differently based on object or context. It mainly includes compile-time and runtime polymorphism.

polymorphism
Polymorphism in Python

Let's discuss each type in detail.

1. Compile-time Polymorphism

Compile-time polymorphism, also known as static polymorphism, occurs when the method or operation to be executed is determined before program execution. Languages such as Java and C++ support it through method overloading, whereas Python does not directly support traditional compile-time method overloading.

Example: This code demonstrates overloading-like behavior using variable length arguements.

Python
class Calculator:
    def add(self, *numbers):
        return sum(numbers)


calc = Calculator()

print(calc.add(5))
print(calc.add(5, 10))
print(calc.add(5, 10, 15))

Output
5
15
30

Explanation:

  • *numbers parameter allows the method to accept any number of arguments.
  • sum() function adds all the supplied values.
  • Same method handles different numbers of arguments without defining multiple methods.
  • Provides overloading-like flexibility but is not true compile-time polymorphism.

2. Runtime Polymorphism (Overriding)

Also known as dynamic polymorphism, occurs when the method implementation is determined during program execution based on the actual object. Python achieves this through method overriding, where child classes provide their own implementations of inherited methods.

Example: The following code demonstrates runtime polymorphism using method overriding.

Python
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")


animals = [Dog(), Cat(), Animal()]

for animal in animals:
    animal.sound()

Output
Dog barks
Cat meows
Animal makes a sound

Explanation:

  • Animal defines the sound() method inherited by Dog and Cat.
  • Both child classes override the inherited method with their own implementations.
  • Same method call produces different outputs depending on the object.
  • Python selects the appropriate implementation at runtime.

Built-in Functions

Python's built-in functions such as len() and max() are polymorphic because they work with different data types and return results based on type of object passed. This showcases it's dynamic nature, where same function name adapts its behavior depending on input.

Example: This code demonstrates polymorphism in built-in functions handling strings, lists, numbers and characters differently while using same function name.

Python
print(len("Hello"))
print(len([1, 2, 3]))

print(max(1, 3, 2))
print(max("a", "z", "m"))

Output
5
3
3
z

Explanation:

  • len() returns the number of characters in a string and the number of elements in a list.
  • max() returns the largest value among integers.
  • Same max() function can also compare strings.
  • Each function works with different supported inputs without requiring separate function names.

Polymorphism in Functions

It allows functions to work with different object types as long as they support the required behavior. Using duck typing, it focuses on whether an object has the required methods rather than its type, enabling flexible and reusable code.

Example: This code demonstrates demonstrates how the same function works with different objects using a common method.

Python
class Pen:
    def use(self):
        return "Writing"


class Eraser:
    def use(self):
        return "Erasing"


def perform_task(tool):
    print(tool.use())


perform_task(Pen())
perform_task(Eraser())

Output
Writing
Erasing

Explanation:

  • Pen and Eraser are two different classes.
  • Both classes implement the use() method.
  • perform_task() accepts either object without checking its class.
  • Executed behavior depends on the object passed to the function.

Polymorphism in Operators

Same operator (+) can perform different tasks depending on operand types. This is known as operator overloading. This flexibility is a key aspect of polymorphism.

Example: This code shows operator polymorphism as + operator behaves differently based on data types adding integers, concatenating strings and merging lists all using same operator.

Python
print(5 + 10)

print("Hello " + "World!")

print([1, 2] + [3, 4])

Output
15
Hello World!
[1, 2, 3, 4]

Explanation:

  • The + operator adds two integers.
  • It concatenates two strings into one.
  • It combines the elements of two lists into a new list.
  • Also allows custom classes to define operator behavior using special methods such as add().
Comment