[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Dillon Collins dillonco at comcast.net
Sat Oct 4 21:00:59 CEST 2008


On Friday 03 October 2008, Greg Ewing wrote:
> It's true that with a 'let' statement or equivalent,
> there's no strict need for a change to the for-loop,
> since you can always say
>
>    for i in range(10):
>      let i = i:
>        funcs.append(lambda: i)
>
> But it's an annoying and odd-looking piece of
> boilerplate to have to use, and in that respect is
> similar to the existing solutions of inserting another
> lambda or using a default argument value.
>
> So as a *convenience*, I'm suggesting that the
> for-loop be given automatic let-like behaviour.

For what percentage of your for loops will this matter?  For me, 0, though I 
did have one IIRC before I rewrote it.  I imagine most people are no 
different.

My point is that it's a lot of added complexity, and possible bugs, gotchas, 
and performance penalties for an extremely basic part of the language.  If we 
need to have another keyword anyway, why do we need to deal with all that 
just to save a line in a handful of loops?

> The claim was that there exist side-effectful languages
> with closures that close over values instead of variables.
> C can't be one of those, because it doesn't even have
> closures.

Ah, my apologies; I seem to have totally spaced reading the relevant part of 
the GP.



More information about the Python-ideas mailing list