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

Thomas Lotze thomas at thomas-lotze.de
Tue Jul 12 16:27:41 EDT 2005


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




More information about the Python-list mailing list