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: 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: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.167.107.246; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD References: <507017AB.8090507@davea.name> 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: 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 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")