Skip to content

Commit aab8867

Browse files
committed
gh-65557: Clarify 'only evaluated once' in augmented assignment docs
1 parent 2cd5b79 commit aab8867

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

Doc/reference/simple_stmts.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,15 @@ symbols.)
286286
An augmented assignment evaluates the target (which, unlike normal assignment
287287
statements, cannot be an unpacking) and the expression list, performs the binary
288288
operation specific to the type of assignment on the two operands, and assigns
289-
the result to the original target. The target is only evaluated once.
289+
the result to the original target. Subexpressions of the target are only
290+
evaluated once.
290291

291292
An augmented assignment statement like ``x += 1`` can be rewritten as ``x = x +
292293
1`` to achieve a similar, but not exactly equal effect. In the augmented
293-
version, ``x`` is only evaluated once. Also, when possible, the actual operation
294+
version, subexpressions of ``x`` are only evaluated once: for a target of the
295+
form ``y[k]``, ``y`` and ``k`` are each evaluated only once, but the
296+
subscription may be performed twice, once to fetch the old value and once to
297+
store the result. Also, when possible, the actual operation
294298
is performed *in-place*, meaning that rather than creating a new object and
295299
assigning that to the target, the old object is modified instead.
296300

0 commit comments

Comments
 (0)