白學這麼多年 Python ?連泛型函式都不會寫?
阿新 • • 發佈:2020-10-11
泛型,如果你嘗過java,應該對他不陌生吧。但你可能不知道在 Python 中(3.4+ ),也可以實現 簡單的泛型函式。
在Python中只能實現基於單個(第一個)引數的資料型別來選擇具體的實現方式,官方名稱 是 single-dispatch
。你或許聽不懂,說人話,就是可以實現第一個引數的資料型別不同,其呼叫的函式也就不同。
singledispatch
是 PEP443 中引入的,如果你對此有興趣,PEP443 應該是最好的學習文件:https://www.python.org/dev/peps/pep-0443/
它使用方法極其簡單,只要被singledispatch
裝飾的函式,就是一個single-dispatch
generic functions
)。
- 單分派:根據一個引數的型別,以不同方式執行相同的操作的行為。
- 多分派:可根據多個引數的型別選擇專門的函式的行為。
- 泛函數:多個函式綁在一起組合成一個泛函數。
這邊舉個簡單的例子。
from functools import singledispatch @singledispatch def age(obj): print('請傳入合法型別的引數!') @age.register(int) def _(age): print('我已經{}歲了。'.format(age)) @age.register(str) def _(age): print('I am {} years old.'.format(age)) age(23) # int age('twenty three') # str age(['23']) # list
執行結果
我已經23歲了。
I am twenty three years old.
請傳入合法型別的引數!
說起泛型,其實在 Python 本身的一些內建函式中並不少見,比如 len()
, iter()
,copy.copy()
,pprint()
等
你可能會問,它有什麼用呢?實際上真沒什麼用,你不用它或者不認識它也完全不影響你編碼。
我這裡舉個例子,你可以感受一下。
大家都知道,Python 中有許許多的資料型別,比如 str,list, dict, tuple 等,不同資料型別的拼接方式各不相同,所以我這裡我寫了一個通用的函式,可以根據對應的資料型別對選擇對應的拼接方式拼接,而且不同資料型別我還應該提示無法拼接。以下是簡單的實現。
def check_type(func):
def wrapper(*args):
arg1, arg2 = args[:2]
if type(arg1) != type(arg2):
return '【錯誤】:引數型別不同,無法拼接!!'
return func(*args)
return wrapper
@singledispatch
def add(obj, new_obj):
raise TypeError
@add.register(str)
@check_type
def _(obj, new_obj):
obj += new_obj
return obj
@add.register(list)
@check_type
def _(obj, new_obj):
obj.extend(new_obj)
return obj
@add.register(dict)
@check_type
def _(obj, new_obj):
obj.update(new_obj)
return obj
@add.register(tuple)
@check_type
def _(obj, new_obj):
return (*obj, *new_obj)
print(add('hello',', world'))
print(add([1,2,3], [4,5,6]))
print(add({'name': 'wangbm'}, {'age':25}))
print(add(('apple', 'huawei'), ('vivo', 'oppo')))
# list 和 字串 無法拼接
print(add([1,2,3], '4,5,6'))
輸出結果如下
hello, world
[1, 2, 3, 4, 5, 6]
{'name': 'wangbm', 'age': 25}
('apple', 'huawei', 'vivo', 'oppo')
【錯誤】:引數型別不同,無法拼接!!
如果不使用singledispatch 的話,你可能會寫出這樣的程式碼。
def check_type(func):
def wrapper(*args):
arg1, arg2 = args[:2]
if type(arg1) != type(arg2):
return '【錯誤】:引數型別不同,無法拼接!!'
return func(*args)
return wrapper
@check_type
def add(obj, new_obj):
if isinstance(obj, str) :
obj += new_obj
return obj
if isinstance(obj, list) :
obj.extend(new_obj)
return obj
if isinstance(obj, dict) :
obj.update(new_obj)
return obj
if isinstance(obj, tuple) :
return (*obj, *new_obj)
print(add('hello',', world'))
print(add([1,2,3], [4,5,6]))
print(add({'name': 'wangbm'}, {'age':25}))
print(add(('apple', 'huawei'), ('vivo', 'oppo')))
# list 和 字串 無法拼接
print(add([1,2,3], '4,5,6'))
輸出如下
hello, world
[1, 2, 3, 4, 5, 6]
{'name': 'wangbm', 'age': 25}
('apple', 'huawei', 'vivo', 'oppo')
【錯誤】:引數型別不同,無法拼接!!
推薦下我本人原創的 《PyCharm 中文指南》電子書,內含大量(300張)的圖解,製作之精良,值得每個 Python 工程師點個收藏。