[英]Python: why use overloading instead of *args in a function (especially when the type of arguments do not affect how the function runs)
Edited: I was looking at the type annotations of the built-in zip()
function.编辑:我正在查看内置
zip()
函数的类型注释。
I understand that overload (in the context of type checking), can modify the behaviour of a function depending on the type of parameters it's given.我知道重载(在类型检查的上下文中)可以根据给定的参数类型修改函数的行为。
@overload
def zip(__iter1: Iterable[_T1]) -> List[Tuple[_T1]]: ...
@overload
def zip(__iter1: Iterable[_T1],
__iter2: Iterable[_T2]) -> List[Tuple[_T1, _T2]]: ...
@overload
def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2],
__iter3: Iterable[_T3]) -> List[Tuple[_T1, _T2, _T3]]: ...
@overload
def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3],
__iter4: Iterable[_T4]) -> List[Tuple[_T1, _T2, _T3, _T4]]: ...
@overload
def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3],
__iter4: Iterable[_T4], __iter5: Iterable[_T5]) -> List[Tuple[_T1, _T2, _T3, _T4, _T5]]: ...
@overload
def zip(__iter1: Iterable[Any], __iter2: Iterable[Any], __iter3: Iterable[Any],
__iter4: Iterable[Any], __iter5: Iterable[Any], __iter6: Iterable[Any],
*iterables: Iterable[Any]) -> List[Tuple[Any, ...]]: ...
In this example, however, the type is the same (Any).但是,在此示例中,类型是相同的 (Any)。 So what purpose does overload serve?
那么过载有什么作用呢?
Why not just use the last function, which takes in an arbitrary number of parameters.为什么不直接使用 last 函数,它接受任意数量的参数。 Why create the first five which essentially say: if there's one argument do this, if there's two do that, if there's 3 ... This seems to violate the DRY principle.
为什么创建前五个基本上说:如果有一个参数就这样做,如果有两个就这样做,如果有 3 个......这似乎违反了 DRY 原则。
@overload
def zip(__iter1: Iterable[Any], __iter2: Iterable[Any], __iter3: Iterable[Any],
__iter4: Iterable[Any], __iter5: Iterable[Any], __iter6: Iterable[Any],
*iterables: Iterable[Any]) -> List[Tuple[Any, ...]]: ...
The overload
decorator is for static type analysis, not for implementation. overload
装饰器用于静态类型分析,而不是用于实现。 In fact, the code shown in the question are just the type annotations - zip
is a builtin, it is not implemented in Python.事实上,问题中显示的代码只是类型注释 -
zip
是内置的,它没有在 Python 中实现。
The purpose of the various overloads is to preserve the number and type of arguments.各种重载的目的是保留参数的数量和类型。 For example, it states that a
zip
over three iterables yields tuples with three elements matching the type of the iterable elements.例如,它指出对三个可迭代对象的
zip
生成具有三个与可迭代元素类型匹配的元素的元组。 The final overload with variadic *args
and Any
is merely a catch-all for unspecified cases.可变参数
*args
和Any
的最终重载只是未指定情况的全部内容。
It's an optimization.这是一个优化。 The version that can handle any number of parameters needs to use an extra level of looping.
可以处理任意数量参数的版本需要使用额外的循环级别。 The versions for fixed numbers of arguments can hard-code access to each argument, which is more efficient.
固定数量参数的版本可以硬编码访问每个参数,这样效率更高。
The vast majority of uses of zip()
only have 2-3 arguments, so even though this optimization is probably small, it adds up to be very beneficial. zip()
的绝大多数用途只有 2-3 个参数,因此即使这种优化可能很小,但加起来却非常有益。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.