On Tue, Nov 17, 2020 at 10:16 PM Greg Ewing <greg.ew...@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


>
> So yes, it's all a bit messy, for reasons that are partly historical
> and partly pragmatic, but things seem to work out okay in practice
> most of the time.
>
> If there's anything I would change, it would be to have the for
> statement create a new binding on each iteration, so that capturing
> it with a def or lambda inside the loop works as expected. I even
> came up with a way to do that while still allowing the last-bound
> value to be seen afterwards, but the idea didn't gain any traction.
>
> --
> Greg
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/FC7WYLN7C54ZK2FRQSLH5GFA4P2OHBBC/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/OI2DAQYQT3PWL2K3WSG2TA2U3TN2FYGB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to