A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2014-November/137096.html below:

Change StopIteration handling inside generators

[Python-Dev] PEP 479: Change StopIteration handling inside generators [Python-Dev] PEP 479: Change StopIteration handling inside generatorsEthan 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4