What does the star operator mean, in a function call?

后端 未结 5 2065
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 06:35

What does the * operator mean in Python, such as in code like zip(*x) or f(**k)?

  1. How is it handled internally in the int
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 07:11

    It is called the extended call syntax. From the documentation:

    If the syntax *expression appears in the function call, expression must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments; if there are positional arguments x1,..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

    and:

    If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both expression and as an explicit keyword argument, a TypeError exception is raised.

提交回复
热议问题