Object Oriented Programming (OOP) allows the programmers to add some additional functionalities to the operators and methods which have basic properties. Such a kind of redefining of the entities of the programming structure is called as polymorphism. In earlier chapters, the readers could notice that the addition “+” operator is used for the concatenation of strings inside the print function. This is a kind of polymorphism that is introduced in earlier chapters. Overloading is a type of polymorphism that adds additional functionalities for the existing properties of objects (methods and attributes) or the object itself. Overloading exhibits code reusability and code readability. Overloading is of two types: method overloading (or function overloading) and operator overloading.
Method Overloading
Method overloading is adding additional functionalities to a method of a class by altering the parameters of the methods. In general, method overloading is implemented by defining multiple methods of the same name with
- Varying data types of the parameters.
- A varying number of parameters.
For implementing the method overloading with variable datatypes, the dispatch decorator is used to select different method definition implementations of the same method based on the specified datatypes. The "dispatch" decorator (@dispatch) from the multipledispatch module is one of the famous dispatch decorators used for method overloading. The module multipledispatch is not the default available module in the Python environment, and it should be installed by executing "pip install multipledispatch" in the terminal or command prompt.
zzh@ZZHPC:~$ pip install multipledispatch Collecting multipledispatch Downloading multipledispatch-1.0.0-py3-none-any.whl.metadata (3.8 kB) Downloading multipledispatch-1.0.0-py3-none-any.whl (12 kB) Installing collected packages: multipledispatch Successfully installed multipledispatch-1.0.0 zzh@ZZHPC:~$ python Python 3.12.3 (main, May 16 2024, 09:18:37) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from multipledispatch import dispatch >>>
The syntax for implementing the dispatch operator is shown below.
Before the method definition, @dispatch decorator is used in the above syntax with parameters consisting of datatypes. To specify the data types of the parameters, data of the parameters are given as a parameter to the dispatch decorator. For example,
"@dispatch(int, char, int)
def methodName(i , j, k):
method definition"
The dispatch decorator @dispatch(int, char, int) denotes the parameters of the following method: i, j, and k are int, char, and int data types, respectively. Now, the method would accept only the arguments of datatypes int, char, and int, respectively. So, defining two methods with the same name and changing the parameters of the dispatch operator implements the method overloading by varying datatypes of parameters.
Operator Overloading
Operator overloading is an essential process in OOP, which adds multiple functions to the available operators. Operator overloading is the process of extending the predefined function of an operator to user defined functions. For example, the operator + is used to add two integers, join two strings, and merge two lists. The operator '+' is used for multiple purposes and is thus called operator overloading.
Magic Method
Magic methods are the methods used for operator overloading, which are defined as the method inside a class that is invoked when the corresponding operator is used. Magic methods are also called special functions. There are different kinds of operator in Python, and for each operator, there are unique magic methods for overloading them. Whenever the operator is used with the first operand as the object, the corresponding magic method in its object is called. The list of operators and their corresponding magic methods are listed in the table below. While overloading an operator, the corresponding magic method should be defined with corresponding parameters and functionality in the corresponding class.
Binary Arithmetic Operators
Comparison Operator
Assignment Operators
Unary Operators