slow try statements in python?
Greg Ewing (using news.cis.dfn.de)
me at privacy.net
Mon Feb 17 17:57:21 EST 2003
Mark Higgins wrote:
> I've seen quite a few examples of python code where try statements are
> used to control the flow of a program... seems reasonable, but with my
> background in C++ I'm always concerned that try statements are slow.
>
> Is this a concern in python?
>
Not really. Raising and catching an exception is no more
expensive than many other things that Python programs do
a lot of, e.g. calling methods.
C++ implementations tend to be heavily optimised towards
the case where no exceptions are raised, on the assumption
that exceptions truly are "exceptional". But there is no
such bias in Python -- you're meant to be able to use
exceptions freely.
In fact, exceptions are used heavily under the covers
in places where you might not expect them. Every for-loop,
for example, is terminated by an exception. So if you're
afraid of exceptions, you'd better be afraid of using
for-loops, too!-)
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list