This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author remi.lapeyre
Recipients Adam Cmiel, remi.lapeyre
Date 2020-06-08.15:27:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1591630034.84.0.964675504233.issue40911@roundup.psfhosted.org>
In-reply-to
Content
Multiple steps happens at once here, first the list is extended, then the result is written back to the tuple, at which point it raises TypeError because you can't write to a tuple. When TypeError is raised, the list has already be extended. The code is equivalent to:


a = ([],)
try:
    b = a[0]
    b += [1]
    a[0] = b
except TypeError:
    assert a != ([1],)  # assertion fails
else:
    assert a == ([1],)


This is explained in the documentation at https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements:


An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.

Unlike normal assignments, augmented assignments evaluate the left-hand side before evaluating the right-hand side. For example, a[i] += f(x) first looks-up a[i], then it evaluates f(x) and performs the addition, and lastly, it writes the result back to a[i].
History
Date User Action Args
2020-06-08 15:27:14remi.lapeyresetrecipients: + remi.lapeyre, Adam Cmiel
2020-06-08 15:27:14remi.lapeyresetmessageid: <1591630034.84.0.964675504233.issue40911@roundup.psfhosted.org>
2020-06-08 15:27:14remi.lapeyrelinkissue40911 messages
2020-06-08 15:27:13remi.lapeyrecreate