473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

are variables local only to try/except blocks?

# Is this valid (or is excp local to try/except)?
try:
try:
doSomething1
excp = 0
except:
excp = 1
#endTry
if (_excp_): doSomething1 # is excp defined here?
excp = 0
except:
excp = 1
#endTry
if (excp): doSomething2 # is excp defined here?
# valid, but more verbose (and maybe redundant?)
excp = 0
try:
excp = 0
try:
doSomething1
excp = 0 # reset incase future inner block
except:
excp = 1
#endTry
if (_excp_): doSomething1
excp = 0 # reset incase inner block set excp=1
except:
excp = 1
#endTry
if (excp): doSomething2

I am not so interested in what a particular version of the
Python/Jython interpreter does, but rather what is "right".

Pls "CC" replies to se****@ca.ibm.com (as well as newsgroup)
Barry Searle, se****@ca.ibm.com

Sep 19 '05 #1
2 1802
BarrySearle wrote:
# Is this valid
yes.
(or is excp local to try/except)?
no. try/except doesn't introduce a new scope.
try:
try:
doSomething1
excp = 0
except:
excp = 1
#endTry
if (_excp_): doSomething1 # is excp defined here?
yes (but _excp_ isn't defined, so you need to change that if-statement)
excp = 0
except:
excp = 1
#endTry
if (excp): doSomething2 # is excp defined here?


yes.

</F>

Sep 19 '05 #2
BarrySearle wrote:
# Is this valid (or is excp local to try/except)?
try:
try:
doSomething1
excp = 0
This block is problematic because excp won;t be set if doSomething1
raises an exception.
except:
excp = 1
#endTry
if (_excp_): doSomething1 # is excp defined here?
Presumably you are expecting doSomething1 to fail or succeed in some
non-deterministic way? Otherwise this will just raise the same exception
again!
excp = 0
except:
excp = 1
#endTry
if (excp): doSomething2 # is excp defined here?
# valid, but more verbose (and maybe redundant?)
excp = 0
try:
excp = 0
try:
doSomething1
excp = 0 # reset incase future inner block
except:
excp = 1
#endTry
if (_excp_): doSomething1
excp = 0 # reset incase inner block set excp=1
except:
excp = 1
#endTry
if (excp): doSomething2

I am not so interested in what a particular version of the
Python/Jython interpreter does, but rather what is "right".

Pls "CC" replies to se****@ca.ibm.com (as well as newsgroup)
Barry Searle, se****@ca.ibm.com

Your approach to exception handling is a little simplistic, resulting on
code that reads about as well as a plate of spaghetti.

What are you actually trying to *do*? What problem do you need to solve?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.pycon.org

Sep 20 '05 #3

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

Similar topics

47
3575
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
2
3535
by: Zyron | last post by:
Hello! I'm developing a web page that has a goal of providing personal content, which means that all information should be written in their own language. I'm about to finish my Geo targeting...
7
37366
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
18
3205
by: noridotjabi | last post by:
Whilest I was browesing a tutorial today I came across the infromation that in standard C variables must be decalred at the beginning of a block. Meaning: ...
6
3205
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
21
8070
by: Sriram Rajagopalan | last post by:
Hi, Which of these two would be a better way of initializing the local variables? and why? 1) At the time of their declaration. Eg: void func()
55
6150
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
13
11419
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
26
1740
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
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...
0
7458
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...
0
5578
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,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
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...
0
1512
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 ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.