Consider the following Python 3 code:
a = [-1,-1,-1]
i = 0
And now consider the following two versions of a simultaneous assignment over both a and i:
Assignment version 1:
a[i],i = i,i+1
Assignment version 2:
i,a[i] = i+1,i
I would expect these two versions of simultaneous assignments to be semantically equivalent. However, if you check the values of a and i after each one of the simultaneous assignments, you get different states:
Output for print(a,i)
after assignment version 1:
[0, -1, -1] 1
Output for print(a,i)
after assignment version 2:
[-1, 0, -1] 1
I am not an expert on Python's semantics, but this behaviour seems weird. I would expect both assignments to behave as assignment version 1. Moreover, if you check the following link, one would expect both assignment versions to lead to the same state:
Link to book excerpt in Google Books
Is there something I am missing regarding Python semantics for simultaneous assignments?
Note: This weird behaviour does not seem to be reproducible, for instance, when the variable a
has integer type; it seems to require a
to be of type list (maybe this is the case for any mutable type?).