[Python-Dev] Tricky way of of creating a generator via a comprehension expression (original) (raw)

Guido van Rossum guido at python.org
Sat Nov 25 15:27:57 EST 2017


On Sat, Nov 25, 2017 at 12:17 PM Brett Cannon <brett at python.org> wrote: On Fri, Nov 24, 2017, 19:32 Guido van Rossum, <guido at python.org> wrote:

On Fri, Nov 24, 2017 at 4:22 PM, Guido van Rossum <guido at python.org> wrote:

The more I hear about this topic, the more I think that await, yield and yield from should all be banned from occurring in all comprehensions and generator expressions. That's not much different from disallowing return or break.

From the responses it seems that I tried to simplify things too far. Let's say that await in comprehensions is fine, as long as that comprehension is contained in an async def. While we could save yield_ _[from] in comprehensions, I still see it as mostly a source of confusion, and the fact that the presence of yield [from] implicitly makes the surrounding def a generator makes things worse. It just requires too many mental contortions to figure out what it does. I still propose to rule out all of the above from generator expressions, because those can escape from the surrounding scope. +1 from me On Sat, Nov 25, 2017 at 9:21 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:

So we are keeping asynchronous generator expressions as long as they are defined in an 'async def' coroutine?

I would be happy to declare that await is out of scope for this thread. It seems that it is always well-defined and sensible what it does in comprehensions and in genexprs. (Although I can't help noticing that PEP 530 does not appear to propose await in generator expressions -- it proposes async for in comprehensions and in genexprs, and await in comprehensions only -- but they appear to be accepted nevertheless.)

So we're back to the original issue, which is that yield inside a comprehension accidentally makes it become a generator rather than a list, set or dict. I believe that this can be fixed. But I don't believe we should fix it. I believe we should ban yield from comprehensions and from genexprs. We don't need it, and it's confused most everyone. And the ban should extend to yield from in those same contexts. I think we have a hope for consensus on this.

(I also think that if we had invented await earlier we wouldn't have gone down the path of yield expressions -- but historically it appears we wouldn't have invented await at all if we hadn't first tried yield and then yield from to build coroutines, so I don't think this so bad after all. :-)

-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20171125/25aafc8e/attachment.html>



More information about the Python-Dev mailing list