Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Dark Cowherd
I use Delphi in my day job and evaluating and learning Python over the
weekends and spare time. This thread has been very enlightening to me.

The comments that Joel of Joel on Software makes here
http://www.joelonsoftware.com/items/2003/10/13.html was pretty
convincing. But I can see from the comments made by various people
here that since Python uses Duck typing and encourages typless styles
of functions exceptions may actually be the better way to go.

But one advise that he gives which I think is of great value and is
good practice is
Always catch any possible exception that might be thrown by a library
I'm using on the same line as it is thrown and deal with it
immediately.

DarkCowherd
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Christopher Subich
Dark Cowherd wrote:
 But one advise that he gives which I think is of great value and is
 good practice is
 Always catch any possible exception that might be thrown by a library
 I'm using on the same line as it is thrown and deal with it
 immediately.

That's fine advice, except for when it's not.  Consider the following code:

try:
f = file('file_here')
do_setup_code
do_stuff_with(f)
except IOError: # File doesn't exist
error_handle

To me, this code seems very logical and straightfoward, yet it doesn't 
catch the exception on the very next line following its generation.  It 
relies on the behavior of the rest of the try-block being skipped -- the 
implicit goto that Joel seems to loathe.  If we had to catch it on the 
same line, the only alternative that comes to mind is:
try: f=file('file_here')
except IOError: #File doesn't exist
error_handle
error_flag = 1
if not error_flag:
do_setup_code
do_stuff_with(f)

which nests on weird, arbitrary error flags, and doesn't seem like good 
programming to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Thomas Lotze
Christopher Subich wrote:

 try:
 f=file('file_here')
 except IOError: #File doesn't exist
 error_handle
 error_flag = 1
 if not error_flag:
 do_setup_code
 do_stuff_with(f)
 
 which nests on weird, arbitrary error flags, and doesn't seem like good
 programming to me.

Neither does it to me. What about

try:
f=file('file_here')
except IOError: #File doesn't exist
error_handle
else:
do_setup_code
do_stuff_with(f)

(Not that I'd want to defend Joel's article, mind you...)

-- 
Thomas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Roy Smith
Christopher Subich  [EMAIL PROTECTED] wrote:
try:
f = file('file_here')
do_setup_code
do_stuff_with(f)
except IOError: # File doesn't exist
error_handle

It's also a good idea to keep try blocks as small as possible, so you
know exactly where the error happened.  Imagine if do_setup_code or
do_stuff_with(f) unexpectedly threw an IOError for some reason totally
unrelated to the file not existing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Mike Meyer
Dark Cowherd [EMAIL PROTECTED] writes:

 But one advise that he gives which I think is of great value and is
 good practice is
 Always catch any possible exception that might be thrown by a library
 I'm using on the same line as it is thrown and deal with it
 immediately.

Yuch. That sort of defeats the *purpose* of exceptions in Python:
letting you get on with the coding, and dealing with the errors when
it's convenient. Consider:

try:
out = file(datafile, wb)
out.write(genData1())
out.write(genData2())
out.write(genData3())
except IOError, msg:
print sys.stderr, Save failed:, msg
if os.path.exists(datafile):
   os.unlink(datafile)

I don't even want to *think* writing the try/except clause for each
line. It reminds me to much of:

if (!(out = open(datafile, w))) {
   /* handle errors */
   return ;
}
if (write(out, genData1()) 0) {
   /* handle errors */
   return ;
}
etc.

Generally, I treat exceptions as exceptional. I catch the ones that
require something to be changed to restore the program or environment
to a sane state - and I catch them when it's convenient. The rest I
catch globally and log, so I can figure out what happened and do
something to prevent it from happening again.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use if or try (as a matter of speed)?

2005-07-12 Thread Christopher Subich
Thomas Lotze wrote:
 Neither does it to me. What about
 
 try:
 f=file('file_here')
 except IOError: #File doesn't exist
 error_handle
 else:
 do_setup_code
 do_stuff_with(f)
 
 (Not that I'd want to defend Joel's article, mind you...)

That works.  I'm still not used to having 'else' available like that.  I 
wonder how Joel advocates managing in C++-likes that don't have a 
try/catch/else semantic.
-- 
http://mail.python.org/mailman/listinfo/python-list