Function Overloading in Python

In this article, we will learn about what is function overloading, and how we can do function overloading in python, some example of which are related to function overloading python

What is Function Overloading

Function overloading is a process in which functions when multiple functions have the same name, but the number of parameters in the function varies .this function is overloaded and this is called function overloading.

Function Overloading in Python

Function overloading, as the name suggests, is the practice of using the same function numerous times with various numbers of arguments. However, the overloading of functions is not supported in Python. If we implement the function overloading code as we do in other languages, an error is thrown. Python lacks a data type for method parameters, which is the cause.

The new function with the same name replaces the prior function with the same name (but with different parameters). Therefore, we now see an error if we attempt to call the original function, which had a different number of parameters while passing a different number of arguments that are defined in the second function. Let’s attempt to comprehend the same.

Code:

# your code goes here

def add(a,b):
    p=a+b
    print(p)
    
def add(a,b,c):
    p=a+b+c
    print(p)
    

# add(2,3)  this will not run it gives error


add(1,2,3)
    

Output

6

Explanation
In the above python program we have two methods or function names as add. The first add function takes two parametre and another add function takes three parameters, so this is method overloading both these have the same name but have different parameters. In python, method overloading is not possible so the second add function (which has three parameters) overrides the first one. so if use add (2,3) it will give an error, now add function takes only three arguments. so if we observe on the output screen we have 6, because add(1,2,3) add these numbers and we got 6 as an output.

Different Ways to Achieve Function Overloading in Python.

Method 1

In this method we can use the argument to make or work the same function differently as per the arguments. this method is not the most efficient method.

Code:

# your code goes here
# your code goes here

def add(datatype, *args):
    if datatype == 'int':
        ans = 0
    if datatype == 'str':
        ans = ''
    # Traverse through the arguments
    for i in args:
        ans = ans + i
    print(ans)
# Integer
add('int', 2, 8)
# String
add('str', 'college ', 'Dekho')

Output

10
college Dekho

Explanation
In the above python program we use argument to make the same function work differently, here we have ‘add’ name function, which also takes datatype variable (as a string) in which if the datatype is ‘str’ then ans = ‘ ’, be the empty string and if the datatype=’int’ then and is initialized with the value 0. after this we traverse on argument simply by ‘for’ loop and add all the arguments. So if we observe on the output screen we have 10 which comes by the addition of integers 2 and 8 and this add function also add two string ‘college’ + ‘Dekho’ which comes on the output screen as a whole string ‘college Dekho’.

Method 2

In this method we can achieve the function overloading by user-defined function using the “None”
Keyword as the default parameter.

Code:

# your code goes here
 
def multiply(a=None, b=None):
    if a != None and b == None:
        print(a)
    # else will be executed if both are available and returns multiplication of two
    else:
        print(a*b)
 
 
# two arguments are passed, returns addition of two
multiply(2, 12)
# only one argument is passed, returns a
multiply(9)

Output

24
9

Explanation
The "multiply" method’s first parameter is set to None. As a result, we will have the choice of calling it with or without a parameter.

The technique determines whether or not both parameters are available.
Since "None" has previously been specified as the default value for each parameter, "None" will remain the value if any values are not given.
We can achieve method overloading by using If-Else statements to verify each parameter separately.
So when only one argument is given it simply prints the given argument and when the arguments are two, multiply(2,12) it gives 24 only console.

Method 3

In this method we use Multiple Dispatch Decorator and it is an efficient method, Multiple Dispatch Decorator can be installed by:

By this command

pip3 install multipledispatch

Code:

from multipledispatch import dispatch
# passing one parameter

@dispatch(int, int)
def product(first, second):
    result = first*second
    print(result)

# passing two parameters

@dispatch(int, int, int)
def product(first, second, third):
    result = first * second * third
    print(result)

# calling product method with 2 arguments
product(2, 5) # this will give output of 10

# calling the product method/function  with 3 arguments 
product(2, 5, 2) # this will give output of 20

Output:

10
20

Explanation
Dispatcher builds an object in the backend that maintains various implementations, and at runtime, it chooses the best method based on the kind and quantity of passed-in parameters.

So in the above python program product function run in both cases, when the argument are two and when the argument are three. So in output, we observe product(2,5) gives 10 which simply multiply the two number and product(2,5,2) which have three arguments and it gives 20 on the console. So this is how function overloading can happen in python.

Summary
Function overloading, also known as method overloading, is a feature in some programming languages that allows a single function or method to have multiple implementations, each with a different number or type of parameters. This allows for more concise and readable code, as well as increased flexibility in how the function can be used. For example, a function that calculates the area of a shape could have multiple implementations for different types of shapes (e.g. circles, rectangles, triangles). The specific implementation to be used would be determined at runtime based on the type of shape being passed as an argument. C++, Java, and C# are some examples of programming languages that support function overloading.

Leave a Reply

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