[python]单分派

前言

Python 不支持方法重载,所以不能使用不同的签名定义函数的变体,以不同的方式处理不同的数据类型。要想实现类似的功能,基本实现方式是使用一串if ... elif ... else,类型较少时还行,如果后面功能扩展会显得冗长。还有种做法是使用标准库的funtools.singledispatch装饰器将普通函数变成泛化函数,可以根据第一个参数的类型,以不同的方式执行相同的操作,这称为单分派。如果根据多个参数选择专门的函数,那就是多分派

示例

from functools import singledispatch
from collections.abc import Mapping

@singledispatch  # singledispatch 装饰器标记的是object类型的基函数
def fun(arg: object):
    print(arg)

@fun.register  # 各个类型的专门函数使用@<base>.register 装饰
def _(arg: int):  # 运行时传入的第一个参数的类型决定何时使用这个函数,所以一般没必要单独写个函数名。
    print(f"int type: {type(arg)}, value: {arg}")

@fun.register
def _(arg: str):
    print(f"str type: {type(arg)}, value: {arg}")

@fun.register
def _(arg: list):
    print(f"list type: {type(arg)}, value: {arg}")

@fun.register
def _(arg: Mapping):
    print(f"Mapping type: {type(arg)}, value: {arg}")

if __name__ == "__main__":
    fun(1)
    fun("1")
    fun([1, 2, 3])
    fun({"a": 1, "b": 2})
    fun(1.0)  # 如果没有对应的专门函数,则会调用基函数

运行输出

int type: <class 'int'>, value: 1
str type: <class 'str'>, value: 1
list type: <class 'list'>, value: [1, 2, 3]
Mapping type: <class 'dict'>, value: {'a': 1, 'b': 2}
1.0

应尽量注册处理抽象基类的专门函数,例如numbers.Integralabc.MutableSequence,而不直接处理具体实现,例如intlist。这样的话,代码支持的兼容类型更广泛。

singledispatch的一个显著特征是,你可以在项目代码的任何地方和任何模块中注册专门函数。如果。如果后来在新模块中定义了新类型,则可以轻易添加一个新的自定义函数来处理新类型。此外,还可以为不是自己编写的或者修改的类编写自定义函数。

第三方库multipledispatch

第三方库 multipledispatch 也可以实现类似功能。官方文档地址:https://multiple-dispatch.readthedocs.io/en/latest/

安装

python -m pip install multipledispatch

使用示例

from multipledispatch import dispatch

@dispatch(int, int)
def add(x, y):
	return x + y

@dispatch(object, object)
def add(x, y):
	return f"{x} + {y}"


add(1,2) # 3
add(1, "hello") # "1 + hello"