[python]单分派
<h2 id="前言">前言</h2><p>Python 不支持方法重载,所以不能使用不同的签名定义函数的变体,以不同的方式处理不同的数据类型。要想实现类似的功能,基本实现方式是使用一串<code>if ... elif ... else</code>,类型较少时还行,如果后面功能扩展会显得冗长。还有种做法是使用标准库的<code>funtools.singledispatch</code>装饰器将普通函数变成泛化函数,可以根据第一个参数的类型,以不同的方式执行相同的操作,这称为<strong>单分派</strong>。如果根据多个参数选择专门的函数,那就是<strong>多分派</strong>。</p>
<h2 id="示例">示例</h2>
<pre><code class="language-python">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()
fun({"a": 1, "b": 2})
fun(1.0)# 如果没有对应的专门函数,则会调用基函数
</code></pre>
<p>运行输出</p>
<pre><code>int type: <class 'int'>, value: 1
str type: <class 'str'>, value: 1
list type: <class 'list'>, value:
Mapping type: <class 'dict'>, value: {'a': 1, 'b': 2}
1.0
</code></pre>
<p>应尽量注册处理抽象基类的专门函数,例如<code>numbers.Integral</code>和<code>abc.MutableSequence</code>,而不直接处理具体实现,例如<code>int</code>和<code>list</code>。这样的话,代码支持的兼容类型更广泛。</p>
<p><code>singledispatch</code>的一个显著特征是,你可以在项目代码的任何地方和任何模块中注册专门函数。如果。如果后来在新模块中定义了新类型,则可以轻易添加一个新的自定义函数来处理新类型。此外,还可以为不是自己编写的或者修改的类编写自定义函数。</p>
<h2 id="第三方库multipledispatch">第三方库multipledispatch</h2>
<p>第三方库 multipledispatch 也可以实现类似功能。官方文档地址:https://multiple-dispatch.readthedocs.io/en/latest/</p>
<p>安装</p>
<pre><code class="language-bash">python -m pip install multipledispatch
</code></pre>
<p>使用示例</p>
<pre><code class="language-python">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"
</code></pre>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:花酒锄作田,转载请注明原文链接:https://www.cnblogs.com/XY-Heruo/p/18993410</p><br><br>
来源:https://www.cnblogs.com/XY-Heruo/p/18993410
頁:
[1]