Python: why use overloading instead of *args in a function (especially when the type of arguments do not affect how the function runs)

余生长醉 提交于 2021-02-11 06:52:45

问题


Edited: I was looking at the type annotations of the built-in zip() function.

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). So what purpose does overload serve?

  2. Why not just use the last function, which takes in an arbitrary number of parameters. 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.

    @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:


The overload decorator is for static type analysis, not for implementation. In fact, the code shown in the question are just the type annotations - zip is a builtin, it is not implemented in 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. The final overload with variadic *args and Any is merely a catch-all for unspecified cases.




回答2:


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.



来源:https://stackoverflow.com/questions/64951212/python-why-use-overloading-instead-of-args-in-a-function-especially-when-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!