Python 中的單分派泛函數(shù)你真的了解嗎
泛型,如果你學(xué)過Java ,應(yīng)該對(duì)它不陌生吧。但你可能不知道在 Python 中(3.4+ ),也可以實(shí)現(xiàn)簡(jiǎn)單的泛型函數(shù)。
在Python中只能實(shí)現(xiàn)基于單個(gè)(第一個(gè))參數(shù)的數(shù)據(jù)類型來選擇具體的實(shí)現(xiàn)方式,官方名稱 是 single-dispatch
。你或許聽不懂,說簡(jiǎn)單點(diǎn),就是可以實(shí)現(xiàn)第一個(gè)參數(shù)的數(shù)據(jù)類型不同,其調(diào)用的函數(shù)也就不同。
singledispatch
是 PEP443 中引入的,如果你對(duì)此有興趣,PEP443 應(yīng)該是最好的學(xué)習(xí)文檔:
https://www.python.org/dev/peps/pep-0443/
A generic function is composed of multiple functions implementing the same operation for different types. Which implementation should be used during a call is determined by the dispatch algorithm. When the implementation is chosen based on the type of a single argument, this is known as single dispatch.
它使用方法極其簡(jiǎn)單,只要被singledispatch
裝飾的函數(shù),就是一個(gè)單分派的(single-dispatch
)的泛函數(shù)(generic functions
)。
單分派:根據(jù)一個(gè)參數(shù)的類型,以不同方式執(zhí)行相同的操作的行為。
多分派:可根據(jù)多個(gè)參數(shù)的類型選擇專門的函數(shù)的行為。
泛函數(shù):多個(gè)函數(shù)綁在一起組合成一個(gè)泛函數(shù)。
這邊舉個(gè)簡(jiǎn)單的例子,介紹一下使用方法
from functools import singledispatch @singledispatch def age(obj): print('請(qǐng)傳入合法類型的參數(shù)!') @age.register(int) def _(age): print('我已經(jīng){}歲了。'.format(age)) @age.register(str) def _(age): print('I am {} years old.'.format(age)) age(23) # int age('twenty three') # str age(['23']) # list
執(zhí)行結(jié)果
我已經(jīng)23歲了。
I am twenty three years old.
請(qǐng)傳入合法類型的參數(shù)!
說起泛型,其實(shí)在 Python 本身的一些內(nèi)建函數(shù)中并不少見,比如 len()
, iter()
,copy.copy()
,pprint()
等
你可能會(huì)問,它有什么用呢?實(shí)際上真沒什么用,你不用它或者不認(rèn)識(shí)它也完全不影響你編碼。
我這里舉個(gè)例子,你可以感受一下。
大家都知道,Python 中有許許多的數(shù)據(jù)類型,比如 str,list, dict, tuple 等,不同數(shù)據(jù)類型的拼接方式各不相同,所以我這里我寫了一個(gè)通用的函數(shù),可以根據(jù)對(duì)應(yīng)的數(shù)據(jù)類型對(duì)選擇對(duì)應(yīng)的拼接方式拼接,而且不同數(shù)據(jù)類型我還應(yīng)該提示無法拼接。以下是簡(jiǎn)單的實(shí)現(xiàn)。
def check_type(func): def wrapper(*args): arg1, arg2 = args[:2] if type(arg1) != type(arg2): return '【錯(cuò)誤】:參數(shù)類型不同,無法拼接!!' 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'))
輸出結(jié)果如下
hello, world
[1, 2, 3, 4, 5, 6]
{'name': 'wangbm', 'age': 25}
('apple', 'huawei', 'vivo', 'oppo')
【錯(cuò)誤】:參數(shù)類型不同,無法拼接!!
如果不使用singledispatch 的話,你可能會(huì)寫出這樣的代碼。
def check_type(func): def wrapper(*args): arg1, arg2 = args[:2] if type(arg1) != type(arg2): return '【錯(cuò)誤】:參數(shù)類型不同,無法拼接!!' 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')
【錯(cuò)誤】:參數(shù)類型不同,無法拼接!!
以上是我個(gè)人的一些理解,如有誤解誤傳,還請(qǐng)你后臺(tái)留言幫忙指正!
以上就是Python 中的單分派泛函數(shù)你真的了解嗎的詳細(xì)內(nèi)容,更多關(guān)于Python單分派泛函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
對(duì)django 2.x版本中models.ForeignKey()外鍵說明介紹
這篇文章主要介紹了對(duì)django 2.x版本中models.ForeignKey()外鍵說明介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03python正則表達(dá)式之對(duì)號(hào)入座篇
正則表達(dá)式是對(duì)字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個(gè)“規(guī)則字符串”,這個(gè)“規(guī)則字符串”用來表達(dá)對(duì)字符串的一種過濾邏輯2018-07-07Python unittest單元測(cè)試框架及斷言方法
這篇文章主要介紹了Python unittest單元測(cè)試框架及斷言方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04詳解python數(shù)據(jù)結(jié)構(gòu)之隊(duì)列Queue
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之隊(duì)列Queue,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05Python使用pyinstaller打包spec文件的方法詳解
PyInstaller是一個(gè)用于將Python腳本打包成獨(dú)立的可執(zhí)行文件的工具,使用PyInstaller您可以將Python應(yīng)用程序轉(zhuǎn)換為可執(zhí)行文件,而無需用戶安裝Python解釋器或任何額外的庫(kù),這篇文章主要給大家介紹了關(guān)于Python使用pyinstaller打包spec文件的相關(guān)資料,需要的朋友可以參考下2024-08-08Python中使用socks5設(shè)置全局代理的方法示例
這篇文章主要介紹了Python中使用socks5設(shè)置全局代理的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python?Enum枚舉類的定義及使用場(chǎng)景最佳實(shí)踐
枚舉(Enum)是一種有助于提高代碼可讀性和可維護(hù)性的數(shù)據(jù)類型,允許我們?yōu)橐唤M相關(guān)的常量賦予有意義的名字,在Python中,枚舉類(Enum)提供了一種簡(jiǎn)潔而強(qiáng)大的方式來定義和使用枚舉2023-11-11