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~

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to