Assignment statement value

心已入冬 提交于 2019-12-04 22:54:06

It's right there in the syntax:

assignment_stmt ::=  (target_list "=")+ (expression_list | yield_expression)

The tiny + at the end of (target_list "=")+ means "one or more". So the line a = b = c = 2 does not consist of 3 assignment statements, but of a single assignment statement with 3 target lists.

Each target list in turn consist only of a single target (an identifier in this case).

It's also in the text (emphasis mine):

An assignment statement [...] assigns the single resulting object to each of the target lists, from left to right.

This can lead to interesting results:

>>> (a,b) = c = (1,2)
>>> (a, b, c)
(1, 2, (1, 2))

Another fine example:

>>a,b,c  = b = 1,2,3
>>b
(1, 2, 3)
JMoney
a = b = c = 2
b = 3
print a,b,c
>>> 2 3 2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!