It's notable that in the same situation perl will do basically the same thing as python ... unless it detects a dependency (i.e. same variable referenced on both sides), in which case it constructs a temporary list, fills that from the RHS, then evaluates the LHS to get a list of lvalues, then assigns (roughly, I've not been in that part of the VM code for a bit).
i.e. from perl's POV, python's behaviour is an optimisation, which if over-applied introduces a bug ... but, y'know, DWIM vs. regularity etc., I'm just surprised that python's behaviour is sloppier than perl's here :)
Yes, this behavior is spelled out in detail in the Python Language Reference [1], in particular section 6.14 Evaluation Order. It explicitly states that the order of evaluation is left to right with the right hand side of assignments being executed before the left hand side. It further gives this example where expressions are evaluated in the order of their suffixes:
expr3, expr4 = expr1, expr2
Although evaluation order is handled differently by different programming languages, it seems that Python is behaving logically here.
That's precisely the place where I think the description can be improved. With complex assignments, there are two parts: compute the place to store a value, and store the value there. In my mental model, the latter is better called assignment than evaluation.
With Python's evaluation/assignment order, both parts of a complex assignment happen at once--which is the source of the weird behavior in the article (A[0] is assigned to before the subscript of A[A[0] - 1] is evaluated).
Comments
I think it's easier to see what is going on without the nested subscripting:
versusIt's notable that in the same situation perl will do basically the same thing as python ... unless it detects a dependency (i.e. same variable referenced on both sides), in which case it constructs a temporary list, fills that from the RHS, then evaluates the LHS to get a list of lvalues, then assigns (roughly, I've not been in that part of the VM code for a bit).
i.e. from perl's POV, python's behaviour is an optimisation, which if over-applied introduces a bug ... but, y'know, DWIM vs. regularity etc., I'm just surprised that python's behaviour is sloppier than perl's here :)
Does the language specify that things will work this way, or is it undefined / implementation-dependent behavior?
To me, it smells like the result of a sloppy specification.
Yes, this behavior is spelled out in detail in the Python Language Reference [1], in particular section 6.14 Evaluation Order. It explicitly states that the order of evaluation is left to right with the right hand side of assignments being executed before the left hand side. It further gives this example where expressions are evaluated in the order of their suffixes:
Although evaluation order is handled differently by different programming languages, it seems that Python is behaving logically here.[1] https://docs.python.org/3.4/reference/expressions.html#evalu...
I think that specification still can be improved. It is clear about order of evaluation, but not on the order of assignment.
Does it do tmp3 = expr1 tmp4 = expr2
or ? I guess it is the latter, but the text does not make that clear.The first--the latter isn't in order (expr3 is "evaluated" before expr2, which in this case means assignment).
That's precisely the place where I think the description can be improved. With complex assignments, there are two parts: compute the place to store a value, and store the value there. In my mental model, the latter is better called assignment than evaluation.
With Python's evaluation/assignment order, both parts of a complex assignment happen at once--which is the source of the weird behavior in the article (A[0] is assigned to before the subscript of A[A[0] - 1] is evaluated).