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

Ethan Furman ethan at stoneleaf.us
Fri Nov 24 21:09:24 EST 2017


On 11/24/2017 04:22 PM, Guido van Rossum 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`.

For me, the deciding factor would be the the affect upon:

1) the containing function (if any); and
2) the result of the genexp/comprehension

I think of generator expressions / comprehensions as self-contained units of code that will have no (side-)effects upon 
the containing function and/or surrounding code (and a containing function is not necessary), and that each will return 
the type expressed by the syntax.

In other words:

[ l for l in ...]  -> list
{s  for s in ...]  -> set
{k:v for k, v in ...]  -> dict
(g for g in ...]  -> genexp

Since 'yield' and 'yield from' invalidate those, I'm in favor of declaring them syntax errors.

If 'await' does not invalidate the above constraints then I'm fine with allowing them.

--
~Ethan~


More information about the Python-Dev mailing list