[issue30729] Swap doesn't work in some circumstances

Raymond Hettinger report at bugs.python.org
Thu Jun 22 02:17:39 EDT 2017


Raymond Hettinger added the comment:

This is the expected behavior.
The assignments are made left-to-right.
The first use of L2[1] is updated BEFORE the second use as index.

The assignments are equivalent to:
==================================
>>> L1 = [1,3,2,4]
>>> L2 = [1,3,2,4]
>>> tup = L2[L2[1] - 1], L2[1]
>>> tup
(2, 3)
>>> L2[1] = tup[0]
>>> L2[L2[1] - 1] = tup[1]
>>> L2
[1, 3, 2, 4]

Which is the same as you observed
=================================
>>> L1 = [1,3,2,4]
>>> L2 = [1,3,2,4]
>>> L2[1], L2[L2[1] - 1] = L2[L2[1] - 1], L2[1]
>>> L2
[1, 3, 2, 4]

The core issue is that L2[1] is being used twice during the series of assignments.  First it gets updated with L2[1] = 3.  Then L2[1] is used again AFTER it has been updated.

----------
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30729>
_______________________________________


More information about the Python-bugs-list mailing list