(original) (raw)
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 evaluationThis brings such generator expressions in line with a simple translation to
function form::def ():
for x in rage(10):
yield x
gen = () # No exception yet
tng = next(gen) # NameErrorDetecting 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 ():
iterable = iter(rage(10))
yield None
for x in iterable:
yield x
gen = ()
next(gen)
I think "rage" is supposed to be "range" in four places in the
quoted block.