Path: csiph.com!usenet.pasdenom.info!aioe.org!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'languages,': 0.04; 'case.': 0.05; 'key.': 0.07; 'dict': 0.09; 'missing)': 0.09; 'terry': 0.09; 'through.': 0.09; 'dec': 0.15; 'sat,': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'object()': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reedy': 0.16; 'subject:compare': 0.16; 'wrote:': 0.17; 'exceptions': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.28; 'chris': 0.28; 'catching': 0.29; 'received:192.168.1.3': 0.29; 'received:84': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'faster': 0.35; 'pm,': 0.35; 'depends': 0.36; 'flow': 0.36; 'method': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'most': 0.61; 'costs': 0.64; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'checks.': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=O5K7TWBW c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=jlvM8aAObukA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=1pmcVapBfBAA:10 a=N7sOqvBrpwHa6eN1-akA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sat, 08 Dec 2012 17:50:46 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354989048 news.xs4all.nl 6921 [2001:888:2000:d::a6]:50588 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34498 On 2012-12-08 07:17, Chris Angelico wrote: > On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy wrote: >> Unfortunately, catching exceptions may be and often is as slow as the >> redundant check and even multiple redundant checks. > > It depends on how often you're going to catch and how often just flow > through. In Python, as in most other modern languages, exceptions only > cost you when they get thrown. The extra check, though, costs you in > the normal case. > That's where the .get method comes in handy: MISSING = object() ... value = my_dict.get(key, MISSING) if value is not MISSING: ... It could be faster if the dict often doesn't contain the key.