Python Method Overloading

Posted by

Python method overloading is explained in this blog using some of the interesting examples so that you can grasp the concept in a easy way.

Python Method Overloading is by default not available but we will be seeing how to implement it in different way.

Method overloading is a feature of object oriented programming that allows multiple methods with the same name to be defined in a class, as long as they have different parameters.

When a method is called, the compiler determines which version of the method to call based on the number, type, and order of the arguments passed in.

In method overloading, each method has a unique signature, which is defined by the method name and the number, type, and order of its parameters. This allows developers to create methods with the same name that perform similar tasks but with different input parameters. This makes the code more concise, readable, and maintainable.

You might have gone through method overloading concept in other programming languages like C but in python the usage is a bit different.

Unlike some other object-oriented programming languages like Java, Python does not support method overloading in the traditional sense. In Python, you can define multiple functions or methods with the same name, but Python does not select the appropriate version of the method to call based on the number, type, or order of the arguments passed in. Instead, the last version of the method defined with the same name overwrites the previous one.

However, Python provides a way to simulate method overloading using default arguments and variable-length argument lists.

Python selects the appropriate version of the method to call based on the number and type of the arguments passed in.

While this technique allows you to simulate method overloading in Python, it is not as robust or intuitive as the method overloading found in other programming languages.

Therefore, it is generally recommended to use different method names or to document the different behavior of each method if you need to provide multiple versions of the same method in Python.

In our previous tutorial we have seen the operator overloading concept to customize the addition operator to perform addition of two objects rather than numbers.

Python Method Overloading Video Tutorial :

Go through this tutorial for more detailed explanation of this concept.

Let’s create a class

Class A :
    def add(self, n1, n2, n3):
        result n1 + n2 + n3
        return result;

a1 = A()
print(a1.add(1, 2, 3))

output:

So when you try to run this code you will get the below output

6

Now let us try to make use of the method overloading concept and use the same function and send one or more (max 3) parameters but get the accurate calculation done.

Here we will be considering 3 scenarios where parameters differ like 3 passed at once, 2 and finally 1 parameter passed but we don’t get any error.

Class A :
    def add(self, n1 = None, n2 = None, n3 = None):
        if n1 is not None and n2 is not None and n3 is not None: 
            result n1 + n2 + n3
        elif n1 is not None and n2 is not None:
            result n1 + n2 
        else
            result n1
        return result;

Now let us try method overloading

create a object

a1 = A()

Try to pass 3 parameters

print(a1.add(1, 2, 3))

output:

6

Now try to pass 2 parameters

print(a1.add(1, 2))

output:

3

Now finally pass one parameter

print(a1.add(1))

output:

1

If you have any query’s in this tutorial on Python Method Overloading do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..

Go through the video tutorial for detailed implementation.