In other words is
d = {}
d["key"] = len(d)
safe in Python?
I know this is undefined behaviour in C++; where the program might get a reference to the element before computing the value it's going to assign to it. Is this similar in Python or is len(d)
always computed before d.__getitem__("key")
?
Yes, in Python it is safe: the evaluation order of an expression is from left to right, but in an assignment statement the right side is evaluated before the assignment happens. Also an arithmetic expression is evaluated in the arithmetic order of their suffixes.
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
In the following lines, expressions will be evaluated in the arithmetic order of their suffixes: