Method Overloading in Python

There are two or more procedures with the same name, but they differ in the number of parameters, the types of parameters, or both of their parameters. This is known as method overloading, and these techniques are referred to as overloaded methods. 

In this blog, we show how method overloading in Python is accomplished, along with practical examples. You need not worry if you have Python installed on your computer system. Explore the top 5 IDEs for Python programming language that help you practice your codes.

What is Method Overloading in Python?

Python’s version of compile-time polymorphism, known as “method overloading,” allows us to declare many methods with the same name but distinct argument lists inside the same class. Method overloading is one of the important concepts in polymorphism and is an integral part of OOPs concepts in Python.

Unlike other programming languages with object class methods in Java and C++, Python doesn’t support method overloading. Since it treats everything as an object, method overloading is not possible in Python. It uses the most recent definition for the technique among all the definitions with the same name. 

As a result, we can specify multiple methods with the same name, but we are limited to using the most recent one. If we define a method in Python, there are multiple ways to call it, so we can make our code have the same functionality as overloaded functions. 

Example

def sum_number(*args):

    result = 0

    for n in args:

        result += n

    print(“Sum : “, result)

if(__name__ == “__main__”):

    print(“Same as Method Overloading\n”)

    print(“Single Argument    ->”, end = ” “)

    sum_number(20)

    print(“Two Arguments      ->”, end = ” “)

    sum_number(40, 2)

    print(“Multiple Arguments ->”, end = ” “)

    sum_number(10, 20, 30, 40, 50)

Output

Same as Method Overloading

Single Argument    -> Sum :  20

Two Arguments      -> Sum :  42

Multiple Arguments -> Sum :  150

Get aware of the role of Python in artificial intelligence and accelerate your fundamental skills in AI and Python.

Method Overloading in Python using Multiple Dispatch

You may also utilize the module multipledispatch to give your function capabilities to method overloading in Python. (pip3 install multipledispatch) A similar function will be created more than once, and a function decorator will be added just above the function. 

Adding a Function Decorator for Method Overloading in Python

@<decorator_name>  

def function(…):

Referring back to method overloading in Python with MultiDispatch, we can enhance the behavior of the wrapped function without making permanent changes by wrapping another function in the same class. Method overloading in Python is one of the top 55 Python interview questions and answers and it is better to be an expert in this concept. 

The decorators will be used in the following manner to provide the functions and characteristics of method overloading:

from multidispatch import dispatch

@dispatch(int, int) 

     def mul(a, b):

            return a * b

@dispatch(int, int, int)  

      def mul(a, b, c):

             return a * b * c

@dispatch(float, float, float) 

      def mul(a, b, c):

             return a * b * c

print(mul(2.1, 3.4, 5.6))

print(mul(3, 4))

print(mul(2, 3, 4))

Output

39.984

12

24

Thus, we have called the mul function easily here, but with different parameters this time, exactly as we did with the my_sum function. Now that the decorator has defined the data types for each function definition, the function definitions are dynamically dispatched based on the data types and the number of parameters specified in the decorator.

The decorator dynamically sends these definitions—the ones we defined using the decorator—to the functions when they are called because the word dispatch simply means transmit. Discover how the Fibonacci series in Python is performed by reading our recent article.

Advantages of Method Overloading in Python

  • It is possible to logically present several methods as a single method in Python by using method overloading.
    • As an example, our get_area methods are composed of a single method called get_area, which appears to be a single method but can actually be used to compute the area of different shapes depending on the kind of input that is supplied to the function. 
    • In addition to making the code easier to read, this also boosts the programmer’s productivity when writing code. Explore the reason behind the bright future of Python and become a certified Python programmer through SLA.
  • Leveraging method overloading allows us to maintain backward compatibility as well.
    • It means that if we have a method that does complicated calculations, we may add a new requirement to it so that it can potentially do the same complicated computation with a few minor adjustments. 
    • The existence of a new parameter, which is optional, will determine whether the computations are done the old-fashioned way or the new-fashioned way.
  • Method overloading in Python also enhances code reusability.

Conclusion

One technique to invoke the same method several times is method overloading. While all methods will share the same name, there may be three variations: a variation in the number of parameters, the parameters’ data type, and the parameters’ order. Mastering the fundamentals of Python includes method overloading. To learn about method overloading in Python, enroll in our Python training institute in Chennai at SLA.