Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > are variables local only to try/except blocks?

Reply
Thread Tools

are variables local only to try/except blocks?

 
 
BarrySearle
Guest
Posts: n/a
 
      09-19-2005
# 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 http://www.velocityreviews.com/forums/(E-Mail Removed) (as well as newsgroup)
Barry Searle, (E-Mail Removed)

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-19-2005
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>



 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      09-20-2005
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 (E-Mail Removed) (as well as newsgroup)
> Barry Searle, (E-Mail Removed)
>

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

 
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
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
different handling of local variables and instance variables when undefined Tammo Tjarks Ruby 2 09-13-2007 06:29 PM
local variables and global variables king Perl Misc 2 06-27-2006 03:13 PM
Can't use 'local' to find sql server instances on local machine karim ASP .Net 1 06-26-2003 09:17 PM



Advertisments