Python Polymorphism – Tutorial with Examples

Polymorphism is a concept in object-oriented programming that refers to the ability of objects of different types to respond to the same method call with different behaviors. In other words, polymorphism allows objects of different classes to be treated as objects of the same class, as long as they implement the same method or interface.

There are two main types of polymorphism in Python:

  • Duck Typing: In Python, objects are considered to be of a certain type not by the class they belong to, but by the methods and attributes they have. This is known as duck typing, and it’s a key feature of dynamic programming languages like Python.
  • Operator Overloading: Python allows classes to define their own implementations of operators (such as +, -, *, etc.), which allows for more natural and intuitive expressions for operations involving objects of those classes.

Duck Typing in Python

In duck typing, the type of an object is determined by its behavior, not by its class. This means that as long as an object has the required methods and attributes, it can be treated as an object of any type, regardless of its actual class.

For example, consider the following code:

def add(a, b):
    return a + b

print(add(1, 2))
print(add("Hello, ", "world!"))

The output of the above code will be:

3
Hello, world!

Even though the first argument is an integer and the second argument is a string, the add function is able to handle both cases because they both have a + operator defined for them. This is an example of duck typing in action.

Operator Overloading in Python

In Python, classes can define their own implementations of operators by defining special methods with specific names. For example, to overload the + operator, a class can define a method named __add__.

For example, consider the following code that defines a class for a point in 2D space:

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

    def __str__(self):
        return "Point({}, {})".format(self.x, self.y)

    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)

p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2
print(p3)

The output of the above code will be:

Point(4, 6)

In this example, the __add__ method is used to overload the + operator for the Point class, allowing points to be added together to create a new point. This makes it easier and more natural to perform mathematical operations with points, and also makes the code more readable and intuitive for developers who are familiar with the standard mathematical meaning of the + operator.

Another example of operator overloading in Python is with the __lt__ (less than) method, which is used to overload the < operator:

class Circle:
    def __init__(self, radius):
        self.radius = radius

    def __lt__(self, other):
        return self.radius < other.radius

c1 = Circle(1)
c2 = Circle(2)
print(c1 < c2)

The output of the above code will be:

True

In this example, the __lt__ method is used to compare the radii of two circles, allowing the < operator to be used to determine which circle is larger. This makes it easier and more natural to compare the sizes of circles, and also makes the code more readable and intuitive for developers who are familiar with the standard mathematical meaning of the < operator.

Conclusion

Polymorphism is a powerful concept in object-oriented programming that allows objects of different types to be treated as objects of the same type, as long as they implement the same methods or interface. Python supports polymorphism through two main mechanisms: duck typing and operator overloading. Understanding and utilizing polymorphism in your code can lead to more flexible and reusable code, as well as more readable and intuitive code for developers.

Leave a Reply

Your email address will not be published. Required fields are marked *