Is python assignment strictly priced from right to left?

In other words,

d = {}
d["key"] = len(d)

is safe in python?

I know that this behavior is undefined in C ++ ; where the program can get a link to the element before calculating the value that it will assign to it. Is this similar in Python or is it len(d)always calculated before d.__getitem__("key")?

+4
source share
2 answers

Yes, it is safe in Python: the evaluation order of an expression is from left to right, but in the assignment operator, the right side is evaluated before the assignment occurs. Also, an arithmetic expression is evaluated in the arithmetic order of their suffixes.

5.14. Assessment Procedure

Python . , , .

:

+7

, RHS LHS; , LHS , .

https://docs.python.org/3/reference/simple_stmts.html#assignment-statements:

(, , , ) .

, , , .

, LHS; :

  • : . (, ), (, ). .
+1

All Articles