473,573 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

temporary scope change

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).
Apr 19 '06 #1
2 1191
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_co ntain]

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

Apr 19 '06 #2
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.
Apr 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4017
by: Ryan | last post by:
Just a quicky about temporarary tables. If using QA, when you create a temporary table, it gets dropped if you close the query. Otherwise you need to state 'DROP TABLE myTable' so that you can re-run the query without the table being there. Sometimes, you can have quite lengthy SQL statements (in a series) with various drop table sections...
11
16280
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the equivalent to the SQL 'with' using TSQL? If there is not one, what is the TSQL solution to creating a temporary table that is associated with an SQL...
3
1182
by: Neelesh Bodas | last post by:
Hello all, I had a confusion regarding temporaries : #include <iostream> class X { public: X() { std::cout << " ctor " << std::endl; } ~X() { std::cout << " dtor " << std::endl; } X(const X&) { std::cout << " copy ctor " << std::endl; } };
7
2296
by: Chad Zalkin | last post by:
We are evaluating some old code that was written as part of our math library. This code uses some optimizations that I'm not sure are necessary or safe, but is a source of debate between my coworkers. Method 1 includes a temporary storage varriable at class scope. Method 2 includes a temporary storage varriable at method scope. Method 3...
14
6496
by: gerald.dalley | last post by:
I've been trying to pin down the scoping rules for temporary variables in C++. I know that doing something like: string s("abc"); const char *t = (s+"def").c_str(); cout << t; is invalid since (s+"def") creates a temporary which goes out of scope, thus leaving t a dangling pointer. What I'm wondering is whether temporaries go out of...
5
2052
by: Ivan Novick | last post by:
Is the string below automatic storage duration? #include <iostream> #include <string> int main(int argc, char** argv) { std::cout << std::string().size() << std::endl; return 0; }
3
2258
by: RainBow | last post by:
I understand that a compiler synthesises a default constructor if none is provided by the user ( of course depending on the situation if synthesis of such c'tor is actually needed in the program e.g if vptr is needed, default c'tor must be synthesised by compiler). In the class Temporary below, there is no reason why a compiler should...
1
1783
by: martinezfive | last post by:
Hi, According to ISO C++ (7.11/2), "An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default." What about temporary objects used as default function arguments? If I have: class A{}; void g( A &b = A() ){} what is the storage duration of the temporary?
7
1816
by: sh.vipin | last post by:
/* Sample code*/ #include <stdio.h> int a; //line # 3 int a; //line # 4 int main (int argc, char *argv){ printf("a is %d",a); }
0
8016
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8061
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6411
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.