temporary scope change

Edward Elliott nobody at 127.0.0.1
Tue Apr 18 19:26:26 EDT 2006


The only time I miss block delimiters in Python is when I want to 
temporarily change the scope of a block.  Suppose I have this code:

for x in list1:
     i += 1
     for y in list2:
         print x * i

Ignore the semantics for the moment (yes the code is suboptimal).  Say I 
need to disable the for y loop for a moment, but I want to keep the print 
statement.  I'd like to just do this

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.

Changing the indentation here isn't a big deal, but imagine the block 
inside y is very long.  Imagine you're disabling several blocks or multiple 
levels of nested blocks at one time.  It quickly becomes a thorny issue. 
Using a debugger to disable it at run-time doesn't always help either.

This seems a common enough problem that I suspect there's a python way to 
handle it.  I don't see a good way without resorting to block delimiters 
though, so I'm asking here for ideas.

Apologies if this has been covered before.  I did some searches of the 
python docs and newsgroup archives but couldn't find anything relevant 
(which may say more about my searching abilities than anything else).



More information about the Python-list mailing list