Python | Method Overloading
Method Overloading:
Two or more methods have the same name but different numbers of parameters or different types of parameters, or both. These methods are called overloaded methods and this is called method overloading.
Like other languages (for example, method overloading in C++) do, python does not support method overloading by default. But there are different ways to achieve method overloading in Python.
The problem with method overloading in Python is that we may overload the methods but can only use the latest defined method.
Python3
def product(a, b):
p = a * b
print (p)
def product(a, b, c):
p = a * b * c
print (p)
product( 4 , 5 , 5 )
|
In the above code, we have defined two product methods we can only use the second product method, as python does not support method overloading. We may define many methods of the same name and different arguments, but we can only use the latest defined method. Calling the other method will produce an error. Like here calling product(4,5) will produce an error as the latest defined product method takes three arguments.
Thus, to overcome the above problem we can use different ways to achieve the method overloading.
Method 1 (Not The Most Efficient Method):
We can use the arguments to make the same function work differently i.e. as per the arguments.
Python3
def add(datatype, * args):
if datatype = = 'int' :
answer = 0
if datatype = = 'str' :
answer = ''
for x in args:
answer = answer + x
print (answer)
add( 'int' , 5 , 6 )
add( 'str' , 'Hi ' , 'Geeks' )
|
Method 2 (Not the efficient one):
We can achieve method overloading in python by user defined function using “None” keyword as default parameter.
Code explanation:
The first parameter of “add” method is set to None. This will give us the option to call it with or without a parameter.
When we pass arguments to the add method (Working):
- The method checks if both the parameters are available or not.
- As we have already given default parameter values as “None”, if any of the value is not passed it will remain “None”.
- Using If-Else statements, we can achieve method overloading by checking each parameter as single statement.
Python3
def add(a = None , b = None ):
if a ! = None and b = = None :
print (a)
else :
print (a + b)
add( 2 , 3 )
add( 2 )
|
The problem with above methods is that, it makes code more complex with multiple if/else statement and is not the desired way to achieve the method overloading.
Method 3 (Efficient One):
By Using Multiple Dispatch Decorator
Multiple Dispatch Decorator Can be installed by:
pip3 install multipledispatch
If pip is not installed on your device:
Click here for “Windows”
Click here for “Linux”
Python3
from multipledispatch import dispatch
@dispatch ( int , int )
def product(first, second):
result = first * second
print (result)
@dispatch ( int , int , int )
def product(first, second, third):
result = first * second * third
print (result)
@dispatch ( float , float , float )
def product(first, second, third):
result = first * second * third
print (result)
product( 2 , 3 )
product( 2 , 3 , 2 )
product( 2.2 , 3.4 , 2.3 )
|
Output:
6
12
17.985999999999997
In Backend, Dispatcher creates an object which stores different implementation and on runtime, it selects the appropriate method as the type and number of parameters passed.
Last Updated :
27 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...