[Python-Dev] Tricky way of of creating a generator via a comprehension expression

Ivan Levkivskyi levkivskyi at gmail.com
Thu Nov 23 02:30:42 EST 2017


On 23 November 2017 at 01:00, Yury Selivanov <yselivanov.ml at gmail.com>
wrote:

> On Wed, Nov 22, 2017 at 6:46 PM, Ivan Levkivskyi <levkivskyi at gmail.com>
> wrote:
> [..]
> > Just found another example of intuitive behaviour:
> >
> >>>> async def f():
> > ...     for i in range(3):
> > ...         yield i
> > ...
> >>>> async def g():
> > ...     return [(yield i) async for i in f()]
> > ...
> >>>> g().send(None)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "<stdin>", line 2, in g
> > TypeError: object async_generator can't be used in 'await' expression
> >
> > of course it is obvious for anyone who writes async code, but anyway an
> > interesting example.
>
> I wouldn't say that it's obvious to anyone...
>
> I think this thread has started to discuss the use of 'yield'
> expression in comprehensions, and the outcome of the discussion is
> that everyone thinks that we should deprecate that syntax in 3.7,
> remove in 3.8.  Let's start with that? :)


I am not sure everyone agrees with this. My main obstacle is following,
consider motivation for the `await` part of PEP 530
which is in my understanding is roughly like this:

"People sometimes want to refactor for-loops containing `await` into a
comprehension but that doesn't work (particularly because of the hidden
function scope) - lets fix this"

I don't see how this compare to:

"People sometimes want to refactor for-loops containing `yield` into a
comprehension but that doesn't work (particularly because of the hidden
function scope) - lets make it a SyntaxError"

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171123/37f264fd/attachment-0001.html>


More information about the Python-Dev mailing list