[Python-checkins] peps: Add another example of PEP 479 justification, courtesy of Isaac Schwabacher

chris.angelico python-checkins at python.org
Sat Dec 6 16:45:35 CET 2014


https://hg.python.org/peps/rev/eb54574b1791
changeset: 5635:eb54574b1791
user: Chris Angelico <rosuav at gmail.com>
date: Sun Dec 07 02:45:03 2014 +1100
summary:
 Add another example of PEP 479 justification, courtesy of Isaac Schwabacher
files:
 pep-0479.txt | 19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/pep-0479.txt b/pep-0479.txt
--- a/pep-0479.txt
+++ b/pep-0479.txt
@@ -72,9 +72,9 @@
 print('commit')
 
 def do_it():
- print('Refactored preparations')
+ print('Refactored initial setup')
 yield # Body of with-statement is executed here
- print('Refactored finalization')
+ print('Refactored finalization of successful transaction')
 
 def gene():
 for i in range(2):
@@ -92,7 +92,20 @@
 current behavior this exception will be swallowed by the context
 manager; and, worse, the finalization is silently skipped! Similarly
 problematic behavior occurs when an ``asyncio`` coroutine raises
-``StopIteration``, causing it to terminate silently.
+``StopIteration``, causing it to terminate silently, or when ``next``
+is used to take the first result from an iterator that unexpectedly
+turns out to be empty, for example::
+
+ # using the same context manager as above
+ import pathlib
+
+ with transaction():
+ print('commit file {}'.format(
+ # I can never remember what the README extension is
+ next(pathlib.Path('/some/dir').glob('README*'))))
+
+In both cases, the refactoring abstraction of ``yield from`` breaks
+in the presence of bugs in client code.
 
 Additionally, the proposal reduces the difference between list
 comprehensions and generator expressions, preventing surprises such as
-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /