[issue46550] __slots__ updates despite being read-only

Eryk Sun report at bugs.python.org
Thu Jan 27 10:48:04 EST 2022


Eryk Sun <eryksun at gmail.com> added the comment:

Please read about augmented assignment [1]. In the REPL, use help("+=").

    An augmented assignment evaluates the target (which, unlike normal
    assignment statements, cannot be an unpacking) and the expression
    list, performs the binary operation specific to the type of
    assignment on the two operands, and assigns the result to the
    original target. The target is only evaluated once.

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

---
[1] https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46550>
_______________________________________


More information about the Python-bugs-list mailing list