Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30898

Re: try/except KeyError vs "if name in ..."

From Terry Reedy <tjreedy@udel.edu>
Subject Re: try/except KeyError vs "if name in ..."
Date 2012-10-06 15:42 -0400
References <k4oj0d$qgl$1@thue.elzevir.fr> <507017AB.8090507@davea.name>
Newsgroups comp.lang.python
Message-ID <mailman.1909.1349552576.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/6/2012 7:36 AM, Dave Angel wrote:

> The distinction in performance between the success and failure modes of
> the try/catch isn't nearly as large as one of the other responses might
> lead you to believe.  For example, a for loop generally terminates with
> a raise (of StopIteration exception), and that doesn't convince us to
> replace it with a while loop.

For statement generally loop many times, up to millions of times, 
without an exception being raised, whereas while statements test the 
condition each time around the loop. So the rule 'if failure is rare 
(less than 10-20%) use try', applies here. For if/them versus 
try/except, I don't worry too much about it.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 08:27 +0200
  Re: try/except KeyError vs "if name in ..." Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-06 08:33 +0000
    Re: try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 12:08 +0200
  Re: try/except KeyError vs "if name in ..." "Günther Dietrich" <gd.usenet@spamfence.net> - 2012-10-06 10:49 +0200
    Re: try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 12:09 +0200
  Re: try/except KeyError vs "if name in ..." Dave Angel <d@davea.name> - 2012-10-06 07:36 -0400
  Re: try/except KeyError vs "if name in ..." Terry Reedy <tjreedy@udel.edu> - 2012-10-06 15:42 -0400
    Re: try/except KeyError vs "if name in ..." Ramchandra Apte <maniandram01@gmail.com> - 2012-10-06 21:17 -0700
    Re: try/except KeyError vs "if name in ..." Ramchandra Apte <maniandram01@gmail.com> - 2012-10-06 21:17 -0700

csiph-web