Introduction to Python Overloading
Method overloading is a way where we add new functionality to an already defined function, and in that way, we overload the function. There are many other languages that support method overloading, and Python also supports method overloading. For example, the plus operator is an example of operator overloading where it can add integers as well as strings. Then the minus operator who can subtract integers, as well sets, lists also so, that’s how Python has overloading features in its operator and functions.
The python overloading can be achieved in two ways or instances; they are listed below,
- Overloading of functions or methods ( Function Overloading ).
- Overloading of operators ( Operator Overloading ).
Function Overloading in Python
Method overloading is not an applied concept in python, but it can be achieved through several techniques. First of all, the concept of method overloading can be classified into two different concepts,
- Overloading user-defined functions.
- Overloading default functions.
1. Overloading User-Defined Functions
User-defined function overloading can be achieved in python by setting a parameter or argument value as none. So if an argument is set as none, then the function reacts in one manner when a value is not passed for this argument and in a different way when a value is not passed for the same argument.
Example:
#!/usr/bin/env python
class Overloading_test:
def overloading_function(self , argument1 = 0 , argument2 = 0 ):
if argument1 is not None or argument2 is not None:
print('Method Overloading parameter count :',(int( argument1) + int(argument2)))
else:
print( ' Method Overloading without parameters ' )
# Create instance
object1 = Overloading_test()
# Call the method with no arguments
object1.overloading_function()
# Call the method with 1 argument
object1.overloading_function(1)
# Call the method with 2 argument
object1.overloading_function(1,1)
Output:
2. Overloading Default or Pre-Defined Functions
Overloading built-in functions involve defining the pre-defined function, which is expected to be overloaded in the python class as a special function. So when the pre-defined function is declared a special function in the Python class, the interpreter will use this special function as the declaration for the pre-defined call. The below example explains this process precisely.
Example:
let us consider the len() function, which is used for determining the length of an item; for this example, the below example shows standard use of the length function without implying overloading and posts the corresponding impact.
# Using len() function without method overloading
Lower_Case = [ ' a ' , ' b ' , ' c ', ' d ' , ' e ' , ' f ', ' g ' , ' h ' , ' i ' , ' j ' , ' k ' , ' l ' , ' m ' , ' n ' , ' o ', ' p ' , ' q ' , ' r ', ' s ' , ' t ' , ' u ' , ' v ', ' w ' , ' x ' , ' y ' , ' z ' ]
Upper_Case = [ ' A ' , ' B ' , ' C ', ' D ' , ' E ' , ' F ', ' G ' , ' H ' , ' I ' , ' J ' , ' K ' , ' L ' , ' M ' , ' N ' , ' O ', ' P ' , ' Q ' , ' R ', ' S ' , ' T ' , ' U ' , ' V ', ' W ' , ' X ' , ' Y ' , ' Z ' ]
print(' Collection used for List1 : ', type(Lower_Case))
print(' Collection used for list2 : ', type(Upper_Case))
print(' Total count of Alphabets in list1 : ', len(Lower_Case))
print(' Total count of Alphabets in list2 : ', len(Upper_Case))
Output:
Now let us use method overloading for the len() function in the same code snippet.
class overloading:
def __init__(self,argument1, argument2):
self.argument1 = argument1
self.argument2 = argument2
def __len__(self):
return argument1+argument2
Lower_Case = [ ' a ' , ' b ' , ' c ', ' d ' , ' e ' , ' f ', ' g ' , ' h ' , ' i ' , ' j ' , ' k ' , ' l ' , ' m ' , ' n ' , ' o ', ' p ' , ' q ' , ' r ', ' s ' , ' t ' , ' u ' , ' v ', ' w ' , ' x ' , ' y ' , ' z ' ]
Upper_Case = [ ' A ' , ' B ' , ' C ', ' D ' , ' E ' , ' F ', ' G ' , ' H ' , ' I ' , ' J ' , ' K ' , ' L ' , ' M ' , ' N ' , ' O ', ' P ' , ' Q ' , ' R ', ' S ' , ' T ' , ' U ' , ' V ', ' W ' , ' X ' , ' Y ' , ' Z ' ]
print(' Collection used for List1 : ', type(Lower_Case))
print(' Collection used for list2 : ', type(Upper_Case))
argument1 = len(Lower_Case)
argument2 = len(Upper_Case)
# Using len() function without method overloading
Object1 = overloading(argument1,argument2)
print('Overall length of both the lists : ', len(Object1))
Output:
We can clearly notice that instead of using the usual built-in length functionality, the overloaded special function __len__() is used, which makes the output print the summed length of both lists instead of their individual length.
Operator Overloading in Python
This involves an extended interpretation of an operator more than its original purpose. The most common example is the addition operator ‘+’, where it can be used for usual addition and also for joining two different strings.
Example:
# add two numbers
print(1 + 2)
# concatenate two strings
print("Hello"+"World")
Output:
Advantages
- Increases code reusability.
- Increases clarity of code.
- The complexity involved in the code is vastly reduced.
Conclusion
Overloading plays a vital role in many high-level programming languages; in the case of python, though they are not implied directly, they hold a significant role in avoiding repetitive usage of code.
Recommended Articles
This is a guide to Python Overloading. Here we discuss the user-defined and pre-defined functions along with its operator and advantages. You may also look at the following articles to learn more –