Python Forum
assignment: not an operator nor expression, but x=y=z=3 works fine? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: assignment: not an operator nor expression, but x=y=z=3 works fine? (/thread-15738.html)



assignment: not an operator nor expression, but x=y=z=3 works fine? - jefdaels - Jan-29-2019

Hi,
When I read the Python reference (Assignment statements), the documenation doesn't describe an repeated assingment syntax (as far as my BNF knowledge brings me).

Still, if I code a=b=c=d=3, the variables a b c and d get the value 3

If they were interpreted from left to right (like C#) and the assignment were an expression (operator) with the assigned value as the result, it would be ok, but the same documentation doesn't mention this. Also, the assignment is not found among the operators (nor the operator precedence list)

Although it works and doesn't give me any problems, I'm curious about how this relates to the reference: am I overlooking something? (if I want to use the reference to work out about details, I should know how to interpret that reference, and this doesn't seem the case right now)

Kind regards, and thanks in advance,
Jef Daels


RE: assignment: not an operator nor expression, but x=y=z=3 works fine? - perfringo - Jan-29-2019

You can read detailed answer from StackOverflow: Python Multiple Assignment Statements In One Line

Somewhat loosely related (from documentation you referred):

Quote:Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example a, b = b, a swaps two variables), overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints [0, 2]:

x = [0, 1]
i = 0
i, x[i] = 1, 2         # i is updated, then x[i] is updated
print(x)