Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python assignment operator associativity

Consider the following Python3 program:

a = [0, 0]  
i = 0  
a[i] = i = 1  
print(a, i)  

a = [0, 0]  
i = 0  
i = a[i] = 1  
print(a, i)  

I expected the output to be:

[0, 1] 1
[1, 0] 1

But instead I got:

[1, 0] 1
[0, 1] 1

My question is: is there anything in the Python language specification about associativity of the assignment operator, or is the behavior for the above example undefined?

All I was able to find is that expressions are evaluated form left to right, except that r-value is evaluated first in case of assignment, but that doesn't help.

like image 877
Ivan Sikiric Avatar asked Oct 12 '11 09:10

Ivan Sikiric


People also ask

What is the associativity of assignment operator?

Right-associative operators of the same precedence are evaluated in order from right to left. For example, assignment is right-associative.

What is the associativity of operators in Python?

Associativity of Python Operators Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence. Almost all the operators have left-to-right associativity. For example, multiplication and floor division have the same precedence.

Is assignment operator right-associative?

Any assignment operators are typically right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

Does the assignment operator has left to right associativity?

The assignment operator has left-to-right-to-left associativity, which means that the value of the expression to the left of the assignment operator is evaluated first and that the result is assigned to the operand on the right. A string variable can hold digits such as phone numbers and zip codes.


2 Answers

Short answer: the code is well defined; the order is left-to-right.

Long answer:

First of all, let's get the terminology right. Unlike in some other languages, assignment in Python is a statement, not an operator. This means that you can't use assignment as part of another expression: for example i = (j = 0) is not valid Python code.

The assignment statement is defined to explicitly permit multiple assignment targets (in your example, these are i and a[i]). Each target can be a list, but let's leave that aside.

Where there are multiple assignment targets, the value is assigned from left to right. To quote the documentation:

An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.

like image 179
NPE Avatar answered Sep 19 '22 15:09

NPE


Just to make it clear, since I struggled myself to understand this. The statement:

a = b = c = ... = E

is equivalent to

a = E
b = E
c = E
...
like image 31
Olivier Verdier Avatar answered Sep 17 '22 15:09

Olivier Verdier