温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中单分派泛函数是什么

发布时间:2021-06-22 09:15:10 来源:亿速云 阅读:190 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关Python中单分派泛函数是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

泛型,如果你学过Java ,应该对它不陌生吧。但你可能不知道在 Python 中(3.4+ ),也可以实现简单的泛型函数。

在Python中只能实现基于单个(第一个)参数的数据类型来选择具体的实现方式,官方名称 是 single-dispatch。你或许听不懂,说简单点,就是可以实现第一个参数的数据类型不同,其调用的函数也就不同。

singledispatch 是 PEP443 中引入的,如果你对此有兴趣,PEP443 应该是最好的学习文档:

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.

它使用方法极其简单,只要被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')
【错误】:参数类型不同,无法拼接!!

感谢各位的阅读!关于“Python中单分派泛函数是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI