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. 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/