Commit 95ae9920 authored by R David Murray's avatar R David Murray

#17973: fix technical inaccuracy in faq entry (it now passes doctest).

parent caf3024f
...@@ -1131,7 +1131,7 @@ a tuple points to. ...@@ -1131,7 +1131,7 @@ a tuple points to.
Under the covers, what this augmented assignment statement is doing is Under the covers, what this augmented assignment statement is doing is
approximately this:: approximately this::
>>> result = a_tuple[0].__iadd__(1) >>> result = a_tuple[0] + 1
>>> a_tuple[0] = result >>> a_tuple[0] = result
Traceback (most recent call last): Traceback (most recent call last):
... ...
...@@ -1154,16 +1154,19 @@ that even though there was an error, the append worked:: ...@@ -1154,16 +1154,19 @@ that even though there was an error, the append worked::
>>> a_tuple[0] >>> a_tuple[0]
['foo', 'item'] ['foo', 'item']
To see why this happens, you need to know that for lists, ``__iadd__`` is equivalent To see why this happens, you need to know that (a) if an object implements an
to calling ``extend`` on the list and returning the list. That's why we say ``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
that for lists, ``+=`` is a "shorthand" for ``list.extend``:: is executed, and its return value is what gets used in the assignment statement;
and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list
and returning the list. That's why we say that for lists, ``+=`` is a
"shorthand" for ``list.extend``::
>>> a_list = [] >>> a_list = []
>>> a_list += [1] >>> a_list += [1]
>>> a_list >>> a_list
[1] [1]
is equivalent to:: This is equivalent to::
>>> result = a_list.__iadd__([1]) >>> result = a_list.__iadd__([1])
>>> a_list = result >>> a_list = result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment