Skip to content

Commit 14d7b71

Browse files
committed
#19953: Clarify the wording of the augmented assignment discussion.
Patch by Priya Pappachan, based on suggestions from Terry Reedy and myself.
1 parent 6120739 commit 14d7b71

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

Doc/faq/programming.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ Use a list comprehension::
11031103

11041104
result = [obj.method() for obj in mylist]
11051105

1106+
.. _faq-augmented-assignment-tuple-error:
11061107

11071108
Why does a_tuple[i] += ['item'] raise an exception when the addition works?
11081109
---------------------------------------------------------------------------

Doc/reference/datamodel.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,11 +2023,13 @@ left undefined.
20232023
``&=``, ``^=``, ``|=``). These methods should attempt to do the operation
20242024
in-place (modifying *self*) and return the result (which could be, but does
20252025
not have to be, *self*). If a specific method is not defined, the augmented
2026-
assignment falls back to the normal methods. For instance, to execute the
2027-
statement ``x += y``, where *x* is an instance of a class that has an
2028-
:meth:`__iadd__` method, ``x.__iadd__(y)`` is called. If *x* is an instance
2029-
of a class that does not define a :meth:`__iadd__` method, ``x.__add__(y)``
2030-
and ``y.__radd__(x)`` are considered, as with the evaluation of ``x + y``.
2026+
assignment falls back to the normal methods. For instance, if *x* is an
2027+
instance of a class with an :meth:`__iadd__` method, ``x += y`` is equivalent
2028+
to ``x = x.__iadd__(y)`` . Otherwise, ``x.__add__(y)`` and ``y.__radd__(x)``
2029+
are considered, as with the evaluation of ``x + y``. In certain situations,
2030+
augmented assignment can result in unexpected errors (see
2031+
:ref:`faq-augmented-assignment-tuple-error`), but this behavior is in
2032+
fact part of the data model.
20312033

20322034

20332035
.. method:: object.__neg__(self)

0 commit comments

Comments
 (0)