loop scope

user at domain.invalid user at domain.invalid
Fri Mar 12 06:21:15 CET 2004

Terry Reedy wrote:

> For loops with a break statement, the value of the loop index when the
> break occurs may be the answer sought or needed to calculate it.  For list
> comps which cannot have break, it is an artifact which may disappear in
> 2.4.

If I understand you, I don't think I like this idea. It 
adds complexity to the mental model of a loop. "The 
loop variable disappears when the loop ends unless the 
loop contains a break".

In my opinion, Python should pick a model and stick to 
it. Loop variables are either ordinary variables that 
exist until you explicitly delete them, or they are 
special variables that automagically disappear at the 
end of the loop. I vote for the first one.

Steven D'Aprano

More information about the Python-list mailing list