Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3a5792

Browse files
committed
merge #17973: fix technical inaccuracy in faq entry (it now passes doctest).
2 parents 3ee6dab + 95ae992 commit b3a5792

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

Doc/faq/programming.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ a tuple points to.
11311131
Under the covers, what this augmented assignment statement is doing is
11321132
approximately this::
11331133

1134-
>>> result = a_tuple[0].__iadd__(1)
1134+
>>> result = a_tuple[0] + 1
11351135
>>> a_tuple[0] = result
11361136
Traceback (most recent call last):
11371137
...
@@ -1154,16 +1154,19 @@ that even though there was an error, the append worked::
11541154
>>> a_tuple[0]
11551155
['foo', 'item']
11561156

1157-
To see why this happens, you need to know that for lists, ``__iadd__`` is equivalent
1158-
to calling ``extend`` on the list and returning the list. That's why we say
1159-
that for lists, ``+=`` is a "shorthand" for ``list.extend``::
1157+
To see why this happens, you need to know that (a) if an object implements an
1158+
``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
1159+
is executed, and its return value is what gets used in the assignment statement;
1160+
and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list
1161+
and returning the list. That's why we say that for lists, ``+=`` is a
1162+
"shorthand" for ``list.extend``::
11601163

11611164
>>> a_list = []
11621165
>>> a_list += [1]
11631166
>>> a_list
11641167
[1]
11651168

1166-
is equivalent to::
1169+
This is equivalent to::
11671170

11681171
>>> result = a_list.__iadd__([1])
11691172
>>> a_list = result

0 commit comments

Comments
 (0)