8000 gh-96265: Formatting changes for faq/programming by slateny · Pull Request #98242 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-96265: Formatting changes for faq/programming #98242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
Prev Previous commit
Next Next commit
Add missing method formatting, use non-literal formatting
  • Loading branch information
slateny committed Oct 13, 2022
commit 85796a8a660c62a9cd18ec82c21633b9fd141a02
15 changes: 8 additions & 7 deletions Doc/faq/programming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ works, but this code:
... print(x)
... x += 1

results in an ``UnboundLocalError``:
results in an :exc:`!UnboundLocalError`:

>>> foo()
Traceback (most recent call last):
Expand Down Expand Up @@ -1340,11 +1340,12 @@ that even though there was an error, the append worked::
['foo', 'item']

To see why this happens, you need to know that (a) if an object implements an
``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
:meth:`~object.__iadd__` magic method, it gets called when the ``+=`` augmented
assignment
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 (b) for lists, :meth:`__iadd__` is equivalent to calling :meth:`list.extend` on the list
and returning the list. That's why we say that for lists, ``+=`` is a
"shorthand" for ``list.extend``::
"shorthand" for :meth:`list.extend`::

>>> a_list = []
>>> a_list += [1]
Expand All @@ -1369,7 +1370,7 @@ Thus, in our tuple example what is happening is equivalent to::
...
TypeError: 'tuple' object does not support item assignment

The ``__iadd__`` succeeds, and thus the list is extended, but even though
The :meth:`__iadd__` succeeds, and thus the list is extended, but even though
``result`` points to the same object that ``a_tuple[0]`` already points to,
that final assignment still results in an error, because tuples are immutable.

Expand Down Expand Up @@ -1543,11 +1544,11 @@ Here the ``UpperOut`` class redefines the ``write()`` method to convert the
argument string to uppercase before calling the underlying
``self._outfile.write()`` method. All other methods are delegated to the
underlying ``self._outfile`` object. The delegation is accomplished via the
``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
:meth:`~object.__getattr__` method; consult :ref:`the language reference <attribute-access>`
for more information about controlling attribute access.

Note that for more general cases delegation can get trickier. When attributes
must be set as well as retrieved, the class must define a :meth:`__setattr__`
must be set as well as retrieved, the class must define a :meth:`~object.__setattr__`
method too, and it must do so carefully. The basic implementation of
:meth:`__setattr__` is roughly equivalent to the following::

Expand Down
0