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 multi-dispatch module is one of the famous dispatch decorators used for method overloading. The module "multi dispatch" is not the default available module in the Python environment, and it should be installed by executing "pip install multi dispatch" in the terminal or command prompt.
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.
Binary Arithmetic Operators
Comparison Operator
标签:parameters,overloading,Python,dispatch,Overloading,int,operator,method From: https://www.cnblogs.com/zhangzhihui/p/18253305