为什么 `*a, b = something` 的解包使得 `a` 总是 `list` 类型?
Why does unpacking of `*a, b = something` makes `a` always `list` type?
我只是在测试拆包,
然后我意识到:
*a, b = {1, 2, 3}
使 a
成为 list
,而不是 set
。
作为:
[1, 2]
我试过了:
*a, = {1, 2, 3}
期待 set
,但它仍然变成了 list
,因为:
[1, 2, 3]
甚至还有元组:
*a, = (1, 2, 3)
它做的 list
和 set
做的一样。
出于某种原因,这对我来说似乎不正确,我认为这是故意的。
Possible changes discussed were:
[...]
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.
我只是在测试拆包,
然后我意识到:
*a, b = {1, 2, 3}
使 a
成为 list
,而不是 set
。
作为:
[1, 2]
我试过了:
*a, = {1, 2, 3}
期待 set
,但它仍然变成了 list
,因为:
[1, 2, 3]
甚至还有元组:
*a, = (1, 2, 3)
它做的 list
和 set
做的一样。
出于某种原因,这对我来说似乎不正确,我认为这是故意的。
Possible changes discussed were:
[...]
Try to give the starred target the same type as the source iterable, for example,
b
ina, *b = 'hello'
would be assigned the string'ello'
. This may seem nice, but is impossible to get right consistently with all iterables.