程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How Python functions are overloaded

編輯:Python

What is function overloading ? Simple understanding , Support the definition of multiple functions with the same name , Only the number or type of parameters are different , At call time , The interpreter will be based on the number or type of parameters , Call the corresponding function .

Overloading is a feature implemented in many languages , such as C++、Java etc. , and Python Does not support . This article , Through a few tricks , It can make Python Support similar functions .

When the number of parameters is different

First look at this situation C++ How to realize overloading

#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 If you define a function in a similar way , No mistake. , Only the later function definition will override the previous , Can't 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 ,Python Function parameters are very flexible , We can define only one function to achieve 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
​

Cases with different parameter types

Again , First look at the current situation C++ How is the overloading of implemented

#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 Two types of parameters are supported : Reshaping and floating point . Invocation time , The interpreter will find the appropriate function according to the parameter type .Python To achieve similar functions , Need help functools.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)
​

func Function is functools.singledispatch After decoration , Another two 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 , Calls itself .

Execution results

Other: zzz
Int: 1
Float: 1.2
​

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

Be careful : Different function return values are also a case of overloading , There's nothing better for the time being Python Realization way , So I didn't mention it

Personally feel , Overloading is designed for language flexibility , and Python Function has many ingenious designs , This time to imitate this technology , It's not really necessary , And I feel a little contrary Python Philosophy of . therefore , This article is more about how to imitate , There is not much explanation for the use scenario of overloading .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved