为什么带星号的赋值产生列表而不是元组?

Why does starred assignment produce lists and not tuples?

在python中,我可以这样写:

some_list = [(1, 2, 3), (3, 2, 1)]

for i, *args in some_list:
   print(args)

我将得到下一个输出:

[2, 3]
[2, 1]

当我们使用 *args 作为函数参数时,它被解压成 tuple.

为什么在这种情况下我们会收到 list

这只是一个设计决定。 tuplePEP 3132 中进行了辩论,但以可用性为由被拒绝:

Make the starred target a tuple instead of a list. This would be consistent with a function's *args, but make further processing of the result harder.

同样,使它成为与赋值右侧的可迭代对象相同的类型,被拒绝了:

Try to give the starred target the same type as the source iterable, for example, b in a, *b = 'hello' would be assigned the string 'ello'. This may seem nice, but is impossible to get right consistently with all iterables.

您的例子列在 specification 下的同一 PEP 中。

在那场辩论的 mailing list 中找到了一些推理。

When dealing with an iterator, you don't know the length in advance, so the only way to get a tuple would be to produce a list first and then create a tuple from it.