*tuple 和 **dict 在 Python 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21809112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:40:14  来源:igfitidea点击:

What does *tuple and **dict means in Python?

pythonpython-3.xtuplesnamedtupleiterable-unpacking

提问by heLomaN

As mentioned in PythonCookbook, *can be added before a tuple, and what does *mean here?

正如PythonCookbook中提到的,*可以在元组之前添加,*这里是什么意思?

Chapter 1.18. Mapping Names to Sequence Elements:

第 1.18 章。将名称映射到序列元素:

from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price'])
s = Stock(*rec) 
# here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)

In the same section, **dictpresents:

在同一部分,**dict介绍:

from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
# Create a prototype instance
stock_prototype = Stock('', 0, 0.0, None, None)
# Function to convert a dictionary to a Stock
def dict_to_stock(s):
    return stock_prototype._replace(**s)

What is **'s function here?

**这里的功能是什么?

采纳答案by Elazar

In a function call

在函数调用中

*tmeans "treat the elements of this tuple as positional arguments to this function call."

*t表示“将此元组的元素视为此函数调用的位置参数。”

def foo(x, y):
    print(x, y)

>>> t = (1, 2)
>>> foo(*t)
1 2

Since v3.5, you can also do this in a list/tuple/set literals:

从 v3.5 开始,您还可以在列表/元组/集文字中执行此操作:

>>> [1, *(2, 3), 4]
[1, 2, 3, 4]

**dmeans "treat the key-value pairs in the dictionary as additional named arguments to this function call."

**d表示“将字典中的键值对视为此函数调用的附加命名参数。”

def foo(x, y):
    print(x, y)

>>> d = {'x':1, 'y':2}
>>> foo(**d)
1 2

Since v3.5, you can also do this in a dictionary literals:

从 v3.5 开始,您还可以在字典文字中执行此操作:

>>> d = {'a': 1}
>>> {'b': 2, **d}
{'b': 2, 'a': 1}

In a function signature

在函数签名中

*tmeans "take all additional positional arguments to this function and pack them into this parameter as a tuple."

*t意思是“将所有额外的位置参数带到这个函数中,并将它们作为一个元组打包到这个参数中。”

def foo(*t):
    print(t)

>>> foo(1, 2)
(1, 2)

**dmeans "take all additional named arguments to this function and insert them into this parameter as dictionary entries."

**d意思是“将所有额外的命名参数带到这个函数中,并将它们作为字典条目插入到这个参数中。”

def foo(**d):
    print(d)

>>> foo(x=1, y=2)
{'y': 2, 'x': 1}

In assignments and forloops

在作业和for循环中

*xmeans "consume additional elements in the right hand side", but it doesn't have to be the last item. Note that xwill always be a list:

*x表示“在右侧消耗其他元素”,但它不必是最后一项。请注意,这x将始终是一个列表:

>>> x, *xs = (1, 2, 3, 4)
>>> x
1
>>> xs
[2, 3, 4]

>>> *xs, x = (1, 2, 3, 4)
>>> xs
[1, 2, 3]
>>> x
4

>>> x, *xs, y = (1, 2, 3, 4)
>>> x
1
>>> xs
[2, 3]
>>> y
4

>>> for (x, *y, z) in [ (1, 2, 3, 4) ]: print(x, y, z)
...
1 [2, 3] 4


Note that parameters that appear after a *are keyword-only:

请注意,出现在 a 之后的*参数仅是关键字:

def f(a, *, b): ...

f(1, b=2)  # fine
f(1, 2)    # error: b is keyword-only

Python3.8 added positional-only parameters, meaning parameters that cannot be used as keyword arguments. They are appear before a /(a pun on *for keyword args).

Python3.8 添加了positional-only parameters,表示不能用作关键字参数的参数。它们出现在 a 之前/*关键字 args的双关语)。

def f(a, /, p, *, k): ...

f(  1,   2, k=3)  # fine
f(  1, p=2, k=3)  # fine
f(a=1, p=2, k=3)  # error: a is positional-only