简体   繁体   English

Python:为什么在函数中使用重载而不是 *args(尤其是当参数的类型不影响函数的运行方式时)

[英]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, ...]]: ...
  1. In this example, however, the type is the same (Any).但是,在此示例中,类型是相同的 (Any)。 So what purpose does overload serve?那么过载有什么作用呢?

  2. 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.可变参数*argsAny的最终重载只是未指定情况的全部内容。

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.

相关问题 当没有 arguments 传递给 Function 参数时,如何处理 Python 中的 *args - How to handle the *args in Python when no arguments are passed over to the Function parameter python中的类型函数重载 - type function overloading in python 使用 *args 时对 function 的 arguments 上的容器类型的混淆 - Confusion regarding the container type cast on the arguments of a function when using *args 有没有一种方法可以在Python函数中使用多个1以上的变长参数(* args)? - Is there a way to use more than 1 variable length arguments (*args) in a Python function? Python函数编号不影响它运行多少次? - Python Function number doesn't affect how many times it runs? 为什么Python中的“全部”函数接收数组而不是* args? - Why 'all' function in Python receives array instead of *args? 如何读取“args”中的 arguments 传递给 Python 中的内置 function [来源]? - How do I read the arguments in “args” passed to a builtin function in Python [source]? 如何在函数中使用* args? - How to use *args in a function? 如何在带有 *args 和 **kwargs 的函数调用中使用默认参数? - How to use default arguments in a function call with *args and **kwargs? 如何在我自己的函数的参数中添加“ args =(…)”? - How do I add “args=(…)” into arguments of my own function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM