[Python-ideas] Changing semantics of for-loop variable

Nick Coghlan ncoghlan at gmail.com
Tue Oct 4 16:35:02 CEST 2011


> What worries me with this proposal is that it only affects the loop variable, and not other variables. This makes it easy to introduce subtle bugs when you forget this. I often have code like this when using closures in a loop:
>
>    for val in sequence:
>        other = lookup(val)
>        result.append(lambda val=val, other=other): doit(val, other))
>
> Greg's proposal means that 'val=val' would no longer be needed, but you'd still need to use the default argument trick for other variables. The difference between the behavior for the loop variable and other variables is also relatively hard to explain.

The proposal actually wasn't clear whether it affected all name
bindings in loops or just the iteration variables in for loops.

Either way, the fact that unrolling the loops (even partially to
implement loop-and-a-half without using break) fundamentally changed
the name binding semantics pretty much killed the idea from my point
of view.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list