temporary scope change

Michael Spencer mahs at telcopartners.com
Wed Apr 19 00:27:10 EDT 2006


Edward Elliott wrote:
...
> 
> for x in list1:
>      i += 1
> #   for y in list2:
>          print x * i
> 
> and have the print line execute as part of the for x block.  In other 
> words, I want the block with print to be in the scope of the for x loop. 
> But instead it raises a SyntaxError because the indentation is different.
> 
Just replace:
     for y in list2:
with:
     if True:

But, in a real debugging situation, y would probably be used in the inner loop,
so you could instead, precede the y loop with:
     list2 = [One_object_that_list2_could_contain]

so that that the loop executes once.  Once that works, you might then 
selectively add items to your temporary list2.

Note that neither the `if` nor the `for` statement actually creates a new scope.

Michael




More information about the Python-list mailing list