Unveiling the Magic of Polymorphism in Python: A Comprehensive Guide

Abhishek Jain
2 min readFeb 9, 2024

--

In a very easy way, polymorphism means “many forms.” It’s like having a single button on a remote control that can do different things depending on what device you’re using it for. Similarly, in programming, polymorphism allows one thing (like a function or method) to take on many forms, adapting to different situations. It’s a cool way to make code flexible and versatile!

Types of Polymorphism:

1. Compile-Time (Static) Polymorphism:

  • Also known as method overloading or function overloading.
  • Occurs at compile time based on the number or types of arguments passed to a function.
  • Multiple functions with the same name but different parameter lists can coexist.
class Calculator:
def add(self, x, y):
return x + y

def add(self, x, y, z):
return x + y + z

# Example of method overloading
calc = Calculator()
result1 = calc.add(2, 3) # Calls the first add method
result2 = calc.add(2, 3, 4) # Calls the second add method

An inbuilt function used for compile time polymorphism is len

print(len("HELLO")
print(len[1,2,3,4,5])
print(len({1:'One',2:'Two',3:'Three'}

Operator overloading in Python is a form of compile-time polymorphism. Compile-time polymorphism, also known as static polymorphism, occurs during the compilation phase of the program based on the number or types of arguments passed to a function or operators used with objects.

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __add__(self, other_point):
print("self : ",self.x, self.y)
print("other_point : ",other_point.x, other_point.y)
return Point(self.x + other_point.x, self.y + other_point.y)

# Example usage
point1 = Point(1, 2)
point2 = Point(3, 4)
result_point = point1 + point2 # Calls __add__ method at compile time
print("result_point : ",result_point.x, result_point.y)

print(2+3) # 5

In this example, the + operator is overloaded using the __add__ method. The decision of which method to call (__add__) is made during the compilation phase based on the operator used.

2. Run-Time (Dynamic) Polymorphism:

  • Also known as method overriding.
  • Occurs at runtime based on the actual type of the object.
  • Involves a base class and one or more derived classes with overridden methods.
class Animal:
def speak(self):
return "Animal speaks"

class Dog(Animal):
def speak(self):
return "Dog barks"

# Example of method overriding
animal = Animal()
dog = Dog()

animal_sound = animal.speak() # Calls speak method of Animal class
dog_sound = dog.speak() # Calls overridden speak method of Dog class

In both cases:

  • Compile-Time Polymorphism (Static): Method overloading is an example of compile-time polymorphism as the determination of which method to call is made at compile time based on the provided arguments.
  • Run-Time Polymorphism (Dynamic): Method overriding is an example of run-time polymorphism as the determination of which method to call is made at runtime based on the actual type of the object.

--

--