On 23 November 2017 at 09:11, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Ivan Levkivskyi wrote:
"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"

Personally I'd be fine with removing the implicit function
scope from comprehensions and allowing yield in them, since
the semantics of that are clear.

But I don't see a way to do anything equivalent with
generator expressions. Since the current effect of
yield in a generator expression is pretty useless,
it seems best just to disallow it.

That means a list comprehension won't be equivalent
to list(generator_expression) in all cases, but I
don't think there's any great need for it to be.

I am also fine with this. Generator expressions are indeed less clear.
Also different people have different mental model about them (especially re implicit scope and equivalence to comprehensions).

On the contrary, vast majority agrees that comprehensions are just for-loops without leaking variables.

--
Ivan