不明白为什么解包没有按预期工作
Don't understand why unpacking is not working as expected
Python 无法解压
a = [1,2,3,4]
*m = a; //error
b,*m = a
print(m) //working
请解释为什么前一个不起作用。
根据 PEP-3132, which introduced this "extended iterable unpacking" syntax, and the language reference,"starred" 目标仅在目标为可迭代对象的赋值中有效。
It is also an error to use the starred expression as a lone assignment
target, as in
*a = range(5)
This, however, is valid syntax:
*a, = range(5)
因此,要使其在语法上有效,您可以这样做:
*m, = a
甚至:
[*m] = a
不过请注意,创建列表的浅表副本的惯用方法是使用切片:
m = a[:]
Python 无法解压
a = [1,2,3,4]
*m = a; //error
b,*m = a
print(m) //working
请解释为什么前一个不起作用。
根据 PEP-3132, which introduced this "extended iterable unpacking" syntax, and the language reference,"starred" 目标仅在目标为可迭代对象的赋值中有效。
It is also an error to use the starred expression as a lone assignment target, as in
*a = range(5)
This, however, is valid syntax:
*a, = range(5)
因此,要使其在语法上有效,您可以这样做:
*m, = a
甚至:
[*m] = a
不过请注意,创建列表的浅表副本的惯用方法是使用切片:
m = a[:]