Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > temporary scope change

Reply
Thread Tools

temporary scope change

 
 
Edward Elliott
Guest
Posts: n/a
 
      04-18-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).
 
Reply With Quote
 
 
 
 
Michael Spencer
Guest
Posts: n/a
 
      04-19-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

 
Reply With Quote
 
 
 
 
Edward Elliott
Guest
Posts: n/a
 
      04-19-2006
Michael Spencer wrote:
> Just replace:
> for y in list2:
> with:
> if True:


Of course. I knew it would be blindingly obvious. Sometimes you just
can't shake the blinders off though. Thanks.

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


Good catch. I'm used to thinking block = scope. Old habits die hard I guess.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Scope of temporary variables gerald.dalley@gmail.com C++ 14 10-15-2006 02:13 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments