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

Terry Reedy tjreedy at udel.edu
Sat Oct 4 16:18:41 CEST 2008

Greg Ewing wrote:
tement, which will be familiar to nobody.
> 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.

Whereas I consider the proposed automaticity to be a grave inconvenience 
and confusion factor.  What if I *want* a closure to be over variables, 
as normal, instead of values.

It seems to me that what you want is fine-grained control over scoping, 
or something like that.  I would prefer that you overtly propose and 
argue for some syntax to do that explicitly, instead of sneaking 
something implicit into for-loops.

Or perhaps a different proposal:
Def statements close over variables, as currently.
Lambda expression close over values, as some people seem to expect them 
to.  This expectation seems to be the crux of the purported 'problem'. 
This change would also deal with Guido's example.

Terry Jan Reedy

More information about the Python-ideas mailing list