On 18/11/20 4:36 pm, Larry Hastings wrote:
>
> But,
> the thinking went, you'd never want to examine the last value from a
> list generator, so it was more convenient if it behaved as if it had its
> own scope.
List comprehensions used to leak, but apparently that was considered
surprising by enough people that it was changed.
Generator expressions are a bit different -- the only sane way to
implement them was to make the body implicitly a separate function,
and non-leaking behaviour naturally fell out of that. Making them
leak would have taken extra work just to get something that nobody
really had a good use case for.
The desire to make list comprehensions and generator expressions
behave consistently may have contributed to the decision to change
list comprehensions to be non-leaking.
It did if I remember correctly.
For me, generator expressions are small functions like you say (see, people did get the equivalent of multi-line lambdas, just in a very specific format 😉), and all the comprehensions are just passing a generator expression to the appropriate constructor, e.g. list(), set(), and dict(). In that regard the scoping is consistent to me.
-Brett
Â