找回密码
 立即注册
首页 业界区 业界 [python]单分派

[python]单分派

撒阗奕 4 天前
前言

Python 不支持方法重载,所以不能使用不同的签名定义函数的变体,以不同的方式处理不同的数据类型。要想实现类似的功能,基本实现方式是使用一串if ... elif ... else,类型较少时还行,如果后面功能扩展会显得冗长。还有种做法是使用标准库的funtools.singledispatch装饰器将普通函数变成泛化函数,可以根据第一个参数的类型,以不同的方式执行相同的操作,这称为单分派。如果根据多个参数选择专门的函数,那就是多分派
示例
  1. from functools import singledispatch
  2. from collections.abc import Mapping
  3. @singledispatch  # singledispatch 装饰器标记的是object类型的基函数
  4. def fun(arg: object):
  5.     print(arg)
  6. @fun.register  # 各个类型的专门函数使用@<base>.register 装饰
  7. def _(arg: int):  # 运行时传入的第一个参数的类型决定何时使用这个函数,所以一般没必要单独写个函数名。
  8.     print(f"int type: {type(arg)}, value: {arg}")
  9. @fun.register
  10. def _(arg: str):
  11.     print(f"str type: {type(arg)}, value: {arg}")
  12. @fun.register
  13. def _(arg: list):
  14.     print(f"list type: {type(arg)}, value: {arg}")
  15. @fun.register
  16. def _(arg: Mapping):
  17.     print(f"Mapping type: {type(arg)}, value: {arg}")
  18. if __name__ == "__main__":
  19.     fun(1)
  20.     fun("1")
  21.     fun([1, 2, 3])
  22.     fun({"a": 1, "b": 2})
  23.     fun(1.0)  # 如果没有对应的专门函数,则会调用基函数
复制代码
运行输出
  1. int type: <class 'int'>, value: 1
  2. str type: <class 'str'>, value: 1
  3. list type: <class 'list'>, value: [1, 2, 3]
  4. Mapping type: <class 'dict'>, value: {'a': 1, 'b': 2}
  5. 1.0
复制代码
应尽量注册处理抽象基类的专门函数,例如numbers.Integral和abc.MutableSequence,而不直接处理具体实现,例如int和list。这样的话,代码支持的兼容类型更广泛。
singledispatch的一个显著特征是,你可以在项目代码的任何地方和任何模块中注册专门函数。如果。如果后来在新模块中定义了新类型,则可以轻易添加一个新的自定义函数来处理新类型。此外,还可以为不是自己编写的或者修改的类编写自定义函数。
第三方库multipledispatch

第三方库 multipledispatch 也可以实现类似功能。官方文档地址:https://multiple-dispatch.readthedocs.io/en/latest/
安装
  1. python -m pip install multipledispatch
复制代码
使用示例
  1. from multipledispatch import dispatch
  2. @dispatch(int, int)
  3. def add(x, y):
  4.         return x + y
  5. @dispatch(object, object)
  6. def add(x, y):
  7.         return f"{x} + {y}"
  8. add(1,2) # 3
  9. add(1, "hello") # "1 + hello"
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册