[英]order of unpacking *args in Python
I wonder why variable last
is 5
when i do 我不知道为什么当我做时变量
last
是5
first, *rest, last = 1,2,3,4,5
I thought assignment goes from left to right, thus *rest
will be [2,3,4,5]
, but it actualy is [2,3,4]
And I thought that last will be empty, or this code will cause error, but suprisingly it works, but I dont understand why 我以为赋值从左到右,因此
*rest
将是[2,3,4,5]
,但实际上是[2,3,4]
并且我以为last将为空,否则此代码将导致错误,但令人惊讶的是它有效,但是我不明白为什么
这是仅在Python 3中有效的语法,称为“扩展解压缩”,并且在PEP 3132中定义-https: //www.python.org/dev/peps/pep-3132/
Just because it's unpacking feature. 只是因为它具有拆包功能。
first
and last
are just variables, when *rest
are arguments, so they get everything between first
and last
value in tuple (1,2,3,4,5). 当
*rest
是参数时, first
和last
只是变量,因此它们获得元组(1,2,3,4,5)中first
和last
值之间的所有值。
If you want to have you wrote: 如果您想写:
[2,3,4,5]
then just use: 然后只需使用:
first, *args = 1,2,3,4,5
# first --> 1
# args --> [2,3,4,5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.