On Fri, Nov 27, 2020 at 01:17:07PM +1300, Greg Ewing wrote:
On 27/11/20 2:25 am, Steven D'Aprano wrote:
Block scoping adds semantic and implementation complexity and annoyance, while giving very little benefit.
Yet in *certain situations* it seems that block scoping is what people subconsciously assume. For example, loop variables in list comprehensions, the leaking of which unsettled enough people that it got changed.
I was one of those people who *liked* the fact that list comp variables leaked :-)
I've come around to accept that it's okay that they don't, and that conceptually comprehensions feel like a self-contained scope, more like a function than an ordinary expression. Especially since the walrus operator now allows me to leak variables out of a comprehension if I really need to.
We can have too many scopes as well as too few:
- a single process-wide global scope is too few;
- every (sub-)expression being its own scope is too many;
so we're just arguing about where the Goldilocks Zone is. My argument is that it is *function scope*, with comprehensions considered to be an honorary function for that purpose.