packing unpacking depends on order.

random832 at fastmail.us random832 at fastmail.us
Thu Sep 3 03:17:25 CEST 2015


On Wed, Sep 2, 2015, at 13:26, Sven R. Kunze wrote:
> I agree as well. First evaluate the right side, then assign it to the 
> left side at once.

The behavior, if it's *not* "first evaluating the right side", is
indistinguishable from it by this test. Assigning the RHS of each case
to a separate list yields the same result:

>>> a, b = [1, 2, 3, 4, 5], 1
>>> abb = a[b], b
>>> bab = b, a[b]
>>> b, a[b] = abb
>>> a
[1, 2, 1, 4, 5]
>>> a, b = [1, 2, 3, 4, 5], 1
>>> a[b], b = bab
>>> a
[1, 1, 3, 4, 5]

What's happening is this:

>>> abb = a[b], b # 2, 1
>>> bab = b, a[b] # 1, 2
then
>>> b, a[b] = abb # b = 2; a[2] = 1
or
>>> a[b], b = bab # a[1] = 1; b = 2

The question is what does "assign it to the left side at once" even
*mean* in the presence of subscripts? Build up a list of
object-subscript pairs (evaluating all the subscripts, including if any
may have side effects) before executing any __setitem__?

Why is the right side evaluated first? Why not build up this "assignment
destination list" first, before evaluating the right side? They wouldn't
be distinguished by this test, since the RHS has no side effects, but
it'd be easy enough to devise such a test case.


More information about the Python-list mailing list