[Python-Dev] PEP 572: Assignment Expressions

Glenn Linderman v+python at g.nevcal.com
Tue Apr 17 12:23:14 EDT 2018


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180417/af56c615/attachment.html>


More information about the Python-Dev mailing list