Chris A: Mention Mark Shannon's counterproposal of changing next(). One small copy-edit.

This commit is contained in:
Guido van Rossum 2014-11-26 09:34:12 -08:00
parent a2bb199d1d
commit 1c2c1c0a7a
1 changed files with 20 additions and 2 deletions

View File

@ -143,8 +143,8 @@ under older Python versions as under the new semantics.
This is done by enclosing those places in the generator body where a This is done by enclosing those places in the generator body where a
``StopIteration`` is expected (e.g. bare ``next()`` calls or in some ``StopIteration`` is expected (e.g. bare ``next()`` calls or in some
cases helper functions that are expected to raise ``StopIteration``) cases helper functions that are expected to raise ``StopIteration``)
in a ``try/except`` construct that returns when ``StopIteration`` in a ``try/except`` construct that returns when ``StopIteration`` is
returns. The ``try/except`` construct should appear directly in the raised. The ``try/except`` construct should appear directly in the
generator function; doing this in a helper function that is not itself generator function; doing this in a helper function that is not itself
a generator does not work. If ``raise StopIteration`` occurs directly a generator does not work. If ``raise StopIteration`` occurs directly
in a generator, simply replace it with ``return``. in a generator, simply replace it with ``return``.
@ -364,6 +364,21 @@ another generator and relies on the latter's ``StopIteration`` to
bubble out would still be potentially wrong, depending on the use made bubble out would still be potentially wrong, depending on the use made
of the distinction between the two exception types. of the distinction between the two exception types.
Converting the exception inside next()
--------------------------------------
Mark Shannon suggested [11]_ that the problem could be solved in
``next()`` rather than at the boundary of generator functions. By
having ``next()`` catch ``StopIteration`` and raise instead
``ValueError``, all unexpected ``StopIteration`` bubbling would be
prevented; however, the backward-incompatibility concerns are far
more serious than for the current proposal, as every ``next()`` call
now needs to be rewritten to guard against ``ValueError`` instead of
``StopIteration`` - not to mention that there is no way to write one
block of code which reliably works on multiple versions of Python.
(Using a dedicated exception type, perhaps subclassing ``ValueError``,
would help this; however, all code would still need to be rewritten.)
Criticism Criticism
========= =========
@ -426,6 +441,9 @@ References
.. [10] Tracker issue with Proof-of-Concept patch .. [10] Tracker issue with Proof-of-Concept patch
(http://bugs.python.org/issue22906) (http://bugs.python.org/issue22906)
.. [11] Post from Mark Shannon with alternate proposal
(https://mail.python.org/pipermail/python-dev/2014-November/137129.html)
Copyright Copyright
========= =========