[Python-Dev] elimination of scope bleeding of iteration variables

Greg Ewing greg.ewing at canterbury.ac.nz
Mon May 1 11:34:17 CEST 2006


Nick Coghlan wrote:
> However, the scoping of for loop 
> variables won't change, as the current behaviour is essential for search loops 
> that use a break statement to terminate the loop when the item is found. 

It occurs to me that there's a middle ground here:
leave the loop variable scope alone, but make it
an error to use the same variable in two different
loops at the same time.

e.g.

   for x in stuff:
     if its_what_were_looking_for(x):
       break
   snarfle(x)
   for x in otherstuff:
     dosomethingelse(x)

would be fine, but

   for x in stuff:
     for x in otherstuff:
       dosomethingelse(x)

would be a SyntaxError because the inner loop
is trying to use x while it's still in use by the
outer loop.

--
Greg


More information about the Python-Dev mailing list