summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2021-11-20 00:40:34 (GMT)
committerGitHub <noreply@github.com>2021-11-20 00:40:34 (GMT)
commitbe36e0634060c7d5dee8e8876fb888bbb53d992a (patch)
treedcf28e3105e12b272cdca00de2b260dec6805183 /Doc/reference
parent4c616911b69ce07fb35da1721506bfaba0998c30 (diff)
downloadcpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.zip
cpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.tar.gz
cpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.tar.bz2
bpo-45250: fix docs regarding `__iter__` and iterators being inconsistently required by CPython (GH-29170)
It is now considered a historical accident that e.g. `for` loops and the `iter()` built-in function do not require the iterators they work with to define `__iter__`, only `__next__`.
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/datamodel.rst26
1 files changed, 12 insertions, 14 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 1ecfa81..b04c95f 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -648,13 +648,13 @@ Callable types
A function or method which uses the :keyword:`yield` statement (see section
:ref:`yield`) is called a :dfn:`generator function`. Such a function, when
- called, always returns an iterator object which can be used to execute the
- body of the function: calling the iterator's :meth:`iterator.__next__`
- method will cause the function to execute until it provides a value
- using the :keyword:`!yield` statement. When the function executes a
- :keyword:`return` statement or falls off the end, a :exc:`StopIteration`
- exception is raised and the iterator will have reached the end of the set of
- values to be returned.
+ called, always returns an :term:`iterator` object which can be used to
+ execute the body of the function: calling the iterator's
+ :meth:`iterator.__next__` method will cause the function to execute until
+ it provides a value using the :keyword:`!yield` statement. When the
+ function executes a :keyword:`return` statement or falls off the end, a
+ :exc:`StopIteration` exception is raised and the iterator will have
+ reached the end of the set of values to be returned.
Coroutine functions
.. index::
@@ -674,7 +674,7 @@ Callable types
A function or method which is defined using :keyword:`async def` and
which uses the :keyword:`yield` statement is called a
:dfn:`asynchronous generator function`. Such a function, when called,
- returns an asynchronous iterator object which can be used in an
+ returns an :term:`asynchronous iterator` object which can be used in an
:keyword:`async for` statement to execute the body of the function.
Calling the asynchronous iterator's :meth:`aiterator.__anext__` method
@@ -2499,12 +2499,10 @@ through the object's keys; for sequences, it should iterate through the values.
.. method:: object.__iter__(self)
- This method is called when an iterator is required for a container. This method
- should return a new iterator object that can iterate over all the objects in the
- container. For mappings, it should iterate over the keys of the container.
-
- Iterator objects also need to implement this method; they are required to return
- themselves. For more information on iterator objects, see :ref:`typeiter`.
+ This method is called when an :term:`iterator` is required for a container.
+ This method should return a new iterator object that can iterate over all the
+ objects in the container. For mappings, it should iterate over the keys of
+ the container.
.. method:: object.__reversed__(self)