variables exist

Scott David Daniels Scott.Daniels at Acm.Org
Tue Apr 12 14:51:58 EDT 2005


Brian van den Broek wrote:
>... STeVe stressed that the try/except solution is only really appropriate 
> for cases where the failure to have the variable defined is quite rare. 

Beware: C++ and Java have an immense overhead for exceptions.  Python
has a very lightweight exception mechanism.  You should _very_seldom_
choose exceptions or not on the basis of performance without measuring
the actual use; you are sure to be surprised.

Structure your code to be easiest to understand.  _If_ it is too slow,
investigate ways to improve its performance.  The usual way is to
build better data structures and use better algorithms.  Only in the
core of a frequently used inner loop should you even care about the
cost of exceptions.  Even then, you can often lift the exception
handler around the loop and recover the most necessary performance.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list