On 4/17/2018 12:46 AM, Chris Angelico wrote:
     gen = (x for x in rage(10)) # NameError
     gen = (x for x in 10) # TypeError (not iterable)
     gen = (x for x in range(1/0)) # Exception raised during evaluation

This brings such generator expressions in line with a simple translation to
function form::

     def <genexp>():
         for x in rage(10):
             yield x
     gen = <genexp>() # No exception yet
     tng = next(gen) # NameError

Detecting these errors more quickly is nontrivial. It is, however, the exact
same problem as generator functions currently suffer from, and this proposal
brings the genexp in line with the most natural longhand form.


Open questions
==============

Can the outermost iterable still be evaluated early?
----------------------------------------------------

As of Python 3.7, the outermost iterable in a genexp is evaluated early, and
the result passed to the implicit function as an argument.  With PEP 572, this
would no longer be the case. Can we still, somehow, evaluate it before moving
on? One possible implementation would be::

     gen = (x for x in rage(10))
     # translates to
     def <genexp>():
         iterable = iter(rage(10))
         yield None
         for x in iterable:
             yield x
     gen = <genexp>()
     next(gen)


I think "rage" is supposed to be "range" in four places in the quoted block.
_______________________________________________
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