[Python-checkins] peps: Chris A: Mention Mark Shannon's counterproposal of changing next(). One small
guido.van.rossum
python-checkins at python.org
Wed Nov 26 18:34:36 CET 2014
https://hg.python.org/peps/rev/9695a84dc696
changeset: 5622:9695a84dc696
user: Guido van Rossum <guido at python.org>
date: Wed Nov 26 09:34:12 2014 -0800
summary:
Chris A: Mention Mark Shannon's counterproposal of changing next(). One small copy-edit.
files:
pep-0479.txt | 22 ++++++++++++++++++++--
1 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/pep-0479.txt b/pep-0479.txt
--- a/pep-0479.txt
+++ b/pep-0479.txt
@@ -143,8 +143,8 @@
This is done by enclosing those places in the generator body where a
``StopIteration`` is expected (e.g. bare ``next()`` calls or in some
cases helper functions that are expected to raise ``StopIteration``)
-in a ``try/except`` construct that returns when ``StopIteration``
-returns. The ``try/except`` construct should appear directly in the
+in a ``try/except`` construct that returns when ``StopIteration`` is
+raised. The ``try/except`` construct should appear directly in the
generator function; doing this in a helper function that is not itself
a generator does not work. If ``raise StopIteration`` occurs directly
in a generator, simply replace it with ``return``.
@@ -364,6 +364,21 @@
bubble out would still be potentially wrong, depending on the use made
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
=========
@@ -426,6 +441,9 @@
.. [10] Tracker issue with Proof-of-Concept patch
(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
=========
--
Repository URL: https://hg.python.org/peps
More information about the Python-checkins
mailing list