I wonder why variable last
is 5
when i do
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
这是仅在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).
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.