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


Groups > comp.lang.python > #30908

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

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <maniandram01@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; '(of': 0.07; 'try:': 0.07; 'loop.': 0.09; 'modes': 0.09; 'raised,': 0.09; 'terminates': 0.09; 'terry': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'times,': 0.13; 'applies': 0.15; 'distinction': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'exceptions': 0.22; 'keyerror:': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'nearly': 0.23; 'statement': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'replace': 0.27; "doesn't": 0.28; 'subject:/': 0.28; 'convince': 0.29; 'statements': 0.29; 'generally': 0.32; 'received:google.com': 0.34; 'received:209.85': 0.35; 'except': 0.36; 'test': 0.36; 'too': 0.36; 'october': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'performance': 0.39; 'between': 0.63; 'subject:...': 0.63; 'success': 0.63; 'skip:n 10': 0.63; "'if": 0.84; 'subject:try': 0.84; 'angel': 0.93; 'responses': 0.93
Newsgroups comp.lang.python
Date Sat, 6 Oct 2012 21:17:33 -0700 (PDT)
In-Reply-To <mailman.1909.1349552576.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=122.167.107.246; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD
References <k4oj0d$qgl$1@thue.elzevir.fr> <507017AB.8090507@davea.name> <mailman.1909.1349552576.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 122.167.107.246
MIME-Version 1.0
Subject Re: try/except KeyError vs "if name in ..."
From Ramchandra Apte <maniandram01@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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>
Message-ID <mailman.1916.1349583456.27098.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349583456 news.xs4all.nl 6933 [2001:888:2000:d::a6]:59279
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30908

Show key headers only | View raw


On Sunday, 7 October 2012 01:12:56 UTC+5:30, Terry Reedy  wrote:
> 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

I use try and except when I need to raise exceptions e.g.:

try:
    value = vm.variables[name]
except KeyError:
    raise NameError("variable name not defined in VM's variables")

Back to comp.lang.python | Previous | NextPrevious 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