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 ..."

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-2.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; '(of': 0.07; 'loop.': 0.09; 'modes': 0.09; 'raised,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terminates': 0.09; 'terry': 0.09; 'times,': 0.13; 'applies': 0.15; 'distinction': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'nearly': 0.23; 'statement': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'replace': 0.27; "doesn't": 0.28; 'header:X-Complaints- To:1': 0.28; 'subject:/': 0.28; 'convince': 0.29; 'statements': 0.29; 'generally': 0.32; 'to:addr:python-list': 0.33; 'received:org': 0.36; 'test': 0.36; 'too': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'between': 0.63; 'subject:...': 0.63; 'success': 0.63; "'if": 0.84; 'received:fios.verizon.net': 0.84; 'subject:try': 0.84; 'angel': 0.93; 'responses': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: try/except KeyError vs "if name in ..."
Date Sat, 06 Oct 2012 15:42:31 -0400
References <k4oj0d$qgl$1@thue.elzevir.fr> <507017AB.8090507@davea.name>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <507017AB.8090507@davea.name>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1909.1349552576.27098.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349552576 news.xs4all.nl 6914 [2001:888:2000:d::a6]:33308
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30898

Show key headers only | 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