What is function overloading? A simple understanding is that the definition of multiple functions with the same name is supported, but the number or type of parameters is different. When calling, the interpreter will call the corresponding function according to the number or type of parameters.
Overloading is implemented in many languages, such as C++, Java, etc., but Python does not support it. In this article, through some tips, Python can support similar functions.
Scenarios with different numbers of parameters
First, let's see how C++ implements overloading in this case
#include <iostream> using namespace std; int func(int a) { cout << 'One parameter' << endl; } int func(int a, int b) { cout << 'Two parameters' << endl; } int func(int a, int b, int c) { cout << 'Three parameters' << endl; }
If Python defines a function in a similar way, there will be no errors, but the subsequent function definition will overwrite the previous one and cannot achieve the effect of overloading.
>>> def func(a): ... print('One parameter') ... >>> def func(a, b): ... print('Two parameters') ... >>> def func(a, b, c): ... print('Three parameters') ... >>> func(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: func() missing 2 required positional arguments: 'b' and 'c' >>> func(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: func() missing 1 required positional argument: 'c' >>> func(1, 2, 3) Three parameters
But we know that the formal parameters of Python functions are very flexible, and we can only define one function to implement the same function, just like this
>>> def func(*args): ... if len(args) == 1: ... print('One parameter') ... elif len(args) == 2: ... print('Two parameters') ... elif len(args) == 3: ... print('Three parameters') ... else: ... print('Error') ... >>> func(1) One parameter >>> func(1, 2) Two parameters >>> func(1, 2, 3) Three parameters >>> func(1, 2, 3, 4) Error
Scenarios with different parameters
Similarly, let’s first look at how C++ overload is implemented under the current situation.
#include <iostream> using namespace std; int func(int a) { cout << 'Int: ' << a << endl; } int func(float a) { cout << 'Float: ' << a << endl; }
In the code, func supports two types of parameters: shaping and floating point type. When called, the interpreter will look for the appropriate function based on the parameter type. To implement similar functions, Python requires the help of a decorator.
from functools import singledispatch @singledispatch def func(a): print(f'Other: {a}') @(int) def _(a): print(f'Int: {a}') @(float) def _(a): print(f'Float: {a}') if __name__ == '__main__': func('zzz') func(1) func(1.2)
After the func function is decorated, two other functions are bound according to different parameter types. When the parameter type is shaping or floating point type, call a corresponding function of the bound, otherwise, call itself.
Execution results
Other: zzz
Int: 1
Float: 1.2
It should be noted that this method can only determine the last called function based on the type of the first parameter.
For more details about singledispatch, please refer to the official documentation
/3.6/library/#
Note: Different return values of functions are also a case of overloading. There is no better Python implementation method at the moment, so it is not mentioned.
Personally, I think that overloading is designed for language flexibility, and Python functions have many clever designs. It is actually not necessary to imitate this technology at this time, and it feels a bit contrary to Python's philosophy. Therefore, this article is more about how to imitate, and does not explain much about the usage scenarios of overloading.
The above is the detailed explanation and integration of Python function reloading introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!