Thursday 23 March 2023

Polymorphism in Python


Polymorphism is a concept in object-oriented programming that allows objects of different classes to be used interchangeably. In Python, polymorphism is achieved through method overloading and method overriding. In this blog, we will discuss polymorphism in Python and how it can be used to write more flexible and reusable code.

Method Overloading in Python

Method overloading is a technique in object-oriented programming where a class can have multiple methods with the same name, but different arguments. Python does not support function overloading in the traditional sense. However, you can achieve similar functionality using default argument values, as shown in the previous example.

Here are some real-life examples of how you might use this approach:

A function that calculates the area of a rectangle

def area(length, width=1):

    return length * width

In this example, the area function takes two arguments: length and width. If width is not provided, it defaults to 1, which allows the function to calculate the area of a square (where length and width are the same).

# Calculate the area of a rectangle

print(area(5, 3))    # prints 15

# Calculate the area of a square

print(area(5))       # prints 5

A function that sorts a list of numbers

def sort_numbers(numbers, reverse=False):
    return sorted(numbers, reverse=reverse)

In this example, the sort_numbers function takes a list of numbers and an optional reverse argument, which defaults to False. If reverse is True, the list is sorted in descending order.

# Sort a list of numbers in ascending order

print(sort_numbers([4, 2, 7, 1]))    # prints [1, 2, 4, 7]

# Sort a list of numbers in descending order print(sort_numbers([4, 2, 7, 1], reverse=True))    # prints [7, 4, 2, 1]

These are just a couple of examples of how you can use default argument values to achieve similar functionality to function overloading in Python.

Method Overriding in Python

Method overriding is another polymorphism concept in object-oriented programming where a subclass can provide its implementation of a method that is already defined in its superclass. Let's take an example to illustrate this concept:

class Animal:

    def make_sound(self):

        print('The animal makes a sound')

class Dog(Animal):

    def make_sound(self):

        print('The dog barks')

class Cat(Animal):

    def make_sound(self):

        print('The cat meows')

dog = Dog()

cat = Cat()

dog.make_sound()   # Output: The dog barks

cat.make_sound()   # Output: The cat meows

In the above example, we have defined a base class Animal with a method make_sound. We have then defined two subclasses Dog and Cat that inherit from Animal and override the make_sound method with their implementations.


In conclusion, polymorphism is a powerful concept in object-oriented programming that allows for code reusability and flexibility. 

Please subscribe my youtube channel for latest python tutorials and this article

No comments:

Post a Comment