How Python functions are overloaded

What is function overloading? Simply understand that the definition of multiple functions with the same name is supported, but the number or type of parameters are 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. This article, through some tips, can make Python support similar functions.

Cases with different number of parameters

Let's take a look at 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 functions in a similar way, no error will be reported, but the following function definitions will overwrite the previous ones, which can not 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

However, we know that the formal parameters of Python functions are very flexible. We can define only one function to achieve the same function, 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

Cases with different parameter types

Similarly, let's first look at how the overloading of C + + is implemented in 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: integer and floating point. When called, the interpreter will find the appropriate function according to the parameter type. Python needs functools to implement similar functions Singledispatch decorator.

from functools import singledispatch

@singledispatch
def func(a):
    print(f'Other: {a}')

@func.register(int)
def _(a):
    print(f'Int: {a}')

@func.register(float)
def _(a):
    print(f'Float: {a}')

if __name__ == '__main__':
    func('zzz')
    func(1)
    func(1.2)

The func function is called functools After the single dispatch decoration, two other functions are bound according to different parameter types. When the parameter type is integer or floating point, call a function corresponding to the binding; otherwise, call itself.

results of enforcement

Other: zzz
Int: 1
Float: 1.2

It should be noted that this way can only determine the last call function according to the type of the first parameter.

Note: different function return values are also overloaded. There is no good Python implementation, so it is not mentioned

Personally, overloading is designed for the flexibility of the language, and python functions have many ingenious designs. At this time, it is not necessary to imitate this technology, and it feels that it is somewhat contrary to Python philosophy. Therefore, this article is more about how to imitate, but does not explain much about the use scenario of overloading.

That's all for this sharing. If it's helpful to you, please pay attention before you go. Thank you for reading.

Keywords: PHP

Added by qbox on Thu, 23 Dec 2021 15:16:18 +0200