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

Paul Moore p.f.moore at gmail.com
Wed Nov 22 11:11:05 EST 2017


On 22 November 2017 at 15:56, Yury Selivanov <yselivanov.ml at gmail.com> wrote:

"await" in generator expressions works correct. I explained it here: https://bugs.python.org/issue32113

To clarify a bit more: r = (V for I in X) is equivalent to: def (): for I in X: yield V r = ()

The docs don't actually say that this equivalence is definitive. There's a lot of vagueness - possibly because the equivalence wasn't precise in 2.X (due to name leakage) and it wasn't updated to say that comprehensions are now defined precisely in terms of this equivalence.

But surely this means that:

  1. await isn't allowed in comprehensions/generator expressions, because the dummy function (_ in your expansion above) is not a coroutine function.
  2. yield expressions in a comprehension.generator will yield extra values into the generated list (or the stream returned from the generator epression). That seems wrong to me, at least in terms of how I'd expect such an expression to behave.

So I think there's a problem with treating the equivalence as the definition - it's informative, but not normative.

Making 'await' a SyntaxError is absolutely not an option. Async generator expressions are a shorthand syntax for defining asynchronous generators (PEP 525), and it's already being used in the wild.

But by the logic you just described, await isn't (or shouldn't be) allowed, surely?

Paul



More information about the Python-Dev mailing list