Method Overloading in Python

Method overloading means creating multiple methods with the same name but with different return types or parameters. Using method overloading, you can perform different operations with the same function name by passing different arguments.

 

Like other programming languages, method overloading is not supported in python. If you try to overload a method in python, it won’t raise any compile-time errors. Instead, it will consider using the last defined method. If you try to call the overloaded methods with different arguments, you will get errors at runtime.

 

Following is the example of creating a class with different methods with same name but with different parameters in python.

 

class Calculate:
    def add(self, a, b):
      print("a + b = {}".format(a + b))

    def add(self, a, b, c):
      print("a + b + c = {}".format(a + b + c))

c1 = Calculate()
c1.add(10, 20, 30)
c1.add(10,20)

If you observe the above example, we created a class with two overloaded methods, one with two parameters and another with three parameters. After that, we created an object for Calculate class and calling the add function by sending different arguments.

 

When you execute the above python method overloading example, you will get the result as shown below.

 

a + b + c = 60
Traceback (most recent call last):
File "D:\pythonclasses.py", line 10, in
c1.add(10,20)
TypeError: add() missing 1 required positional argument: 'c'

If you observe the above result, the add function is expecting another parameter. As discussed, python is considering only the last defined method.

How to Implement Method Overloading in Python?

To achieve method overloading in python, you need to create methods either using default arguments or variable-length arguments.

Using Default Arguments

If you create a method with default arguments, you can call that method with a different number of arguments to achieve method overloading.

 

Following is the example of creating a method with default arguments to achieve method overloading in python.

 

class Calculate:
   def add(self, a, b, c = 0):
     if c > 0:
         print("a + b + c = {}".format(a + b + c))
     else:
         print("a + b = {}".format(a + b))

c1 = Calculate()
c1.add(10, 20, 30)
c1.add(10,20)

If you observe the above example, we call the same method(add) by passing different arguments.

 

When you execute the above python method overloading example, it will return the result as shown below.

 

a + b + c = 60
a + b = 30

Using Variable Length Arguments

As discussed, you can use variable-length arguments in the method to achieve method overloading in python. If you create a method with variable length arguments, you can call that method with a different number of arguments.

 

Following is the example of creating a method with variable length arguments to achieve method overloading in python.

 

class Calculate:
   def add(self, *args):
     result = 0
     for param in args:
        result += param
        print("Result: {}".format(result))

c1 = Calculate()
c1.add(10, 20, 30)
c1.add(10,20)

 The above python method overloading example will return the result as shown below.

 

Result: 60
Result: 30

This is how you can achieve the method overloading in python based on your requirements.