
On Wed, Nov 18, 2020 at 7:45 PM Brett Cannon <brett@python.org> wrote:
On Tue, Nov 17, 2020 at 10:16 PM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
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
If match were to create a scope, would making names in that scope nonlocal by default work? (Obvs we'd then need a local declaration, but given global's long-standing presence in the language and the more recent addition of nonlocal it seems like a fairly natural extension).