Rémi Lapeyre <remi.lape...@henki.fr> added the comment:

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].

----------
nosy: +remi.lapeyre -steven.daprano
versions: +Python 3.10, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40911>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to