[Python-Dev] PEP 479: Change StopIteration handling inside generators
Ethan Furman
ethan at stoneleaf.us
Sun Nov 23 17:17:00 CET 2014
On 11/22/2014 12:30 PM, Raymond Hettinger wrote:
Pre-PEP 479:
-----------
> def middleware_generator(source_generator):
> it = source_generator()
> input_value = next(it)
> output_value = do_something_interesting(input_value)
> yield output_value
Post-PEP 479:
------------
> def middleware_generator(source_generator):
> it = source_generator()
> try:
> input_value = next(it)
> except StopIteration:
> return # This causes StopIteration to be reraised
> output_value = do_something_interesting(input_value)
> yield output_value
While I am in favor of PEP 479, and I have to agree with Raymond that this isn't pretty.
Currently, next() accepts an argument of what to return if the iterator is empty. Can we enhance that in some way so
that the overall previous behavior could be retained?
Something like:
def middleware_generator(source_generator):
it = source_generator()
input_value = next(it, gen_exit=True) # or exc_type=GeneratorExit ?
output_value = do_something_interesting(input_value)
yield output_value
Then, if the iterator is empty, instead of raising StopIteration, or returning some value that would then have to be
checked, it could raise some other exception that is understood to be normal generator termination.
--
~Ethan~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20141123/cbf81eed/attachment.sig>
More information about the Python-Dev
mailing list