What does *tuple and **dict mean in Python?(Python 中的 *tuple 和 **dict 是什么意思?)
问题描述
如 PythonCookbook 中所述,可以在元组之前添加 *
.*
在这里是什么意思?
第 1.18 章.将名称映射到序列元素:
在同一部分,**dict
呈现:
**
在这里的作用是什么?
在函数调用中
*t
表示将此可迭代对象的元素视为此函数调用的位置参数."
从 v3.5 开始,您还可以在列表/元组/集合文字中执行此操作:
**d
表示将字典中的键值对视为此函数调用的附加命名参数."
从 v3.5 开始,您还可以在字典文字中执行此操作:
在函数签名中
*t
表示将所有附加的位置参数作为一个元组打包到这个参数中."
**d
表示将所有附加的命名参数带到此函数,并将它们作为字典条目插入到此参数中."
在赋值和 for
循环中
*x
表示消耗右侧的附加元素",但它不必是最后一项.请注意,x
将始终是一个列表:
请注意,出现在 *
之后的参数仅限关键字:
Python3.8 添加了仅位置参数, 表示不能用作关键字参数的参数.它们出现在 /
之前(*
之前的关键字参数的双关语).
As mentioned in PythonCookbook, *
can be added before a tuple. What does *
mean here?
Chapter 1.18. Mapping Names to Sequence Elements:
In the same section, **dict
presents:
What is **
's function here?
In a function call
*t
means "treat the elements of this iterable as positional arguments to this function call."
Since v3.5, you can also do this in a list/tuple/set literals:
**d
means "treat the key-value pairs in the dictionary as additional named arguments to this function call."
Since v3.5, you can also do this in a dictionary literals:
In a function signature
*t
means "take all additional positional arguments to this function and pack them into this parameter as a tuple."
**d
means "take all additional named arguments to this function and insert them into this parameter as dictionary entries."
In assignments and for
loops
*x
means "consume additional elements in the right hand side", but it doesn't have to be the last item. Note that x
will always be a list:
Note that parameters that appear after a *
are keyword-only:
Python3.8 added positional-only parameters, meaning parameters that cannot be used as keyword arguments. They appear before a /
(a pun on *
preceding keyword-only args).
这篇关于Python 中的 *tuple 和 **dict 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!