Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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; 'else:': 0.03; 'example:': 0.03; 'value,': 0.03; 'try:': 0.07; 'handling,': 0.09; 'matched': 0.09; 'propagate': 0.09; 'def': 0.10; 'itself.': 0.11; "wouldn't": 0.11; 'dec': 0.15; 'value.': 0.15; '(assuming': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'line)': 0.16; 'nameerror': 0.16; 'present;': 0.16; 'subject:compare': 0.16; 'subscripting': 0.16; 'wrote:': 0.17; 'otherwise,': 0.20; 'bit': 0.21; 'meant': 0.21; 'exceptions': 0.22; 'keyerror:': 0.22; 'raise': 0.24; 'pass': 0.25; 'header:In- Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'behaviour': 0.29; 'though.': 0.29; 'fri,': 0.30; 'version,': 0.30; 'error': 0.30; 'point': 0.31; 'received:209.85.160.46': 0.32; 'getting': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'previous': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'thomas': 0.62; 'different': 0.63; 'dict,': 0.84; 'rachel': 0.84; 'why?': 0.84; 'hate': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=agI54Bo3dZx7Pn6sVgQ53XnCo7mq+47yz/YtWz2m7hM=; b=V+/j6c3psih9NyXaqkdnHcVYwFjC1k/AnJhoLstsbGu5lAnHU+FV2lS0Arp/KBfA/t ej/uZvy83PPi3uk62BrEQjDuEzP3ITOfZajQ+XyTfUEF+EFnCkLUhqRj+5AxUymJENHl Ay2fIKNF2XTM7Cp/Hkef0QkuJ023xm2kcxfidKaa2fZizX24wB7XS9aEwH98A3KWgQQH H+tiqyN7N+CD8+eXRidJ16pia4mhQUPNkVZdjfCaqabWQzZfzevAMLOqJ1pTGcPSqVvW GH+dD5lYpbXRgDWRdscGfBFngIhsyANc+oky3P240YUTNVPCoGULPQHirS7/ht4swkYL RAfQ== MIME-Version: 1.0 In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Date: Fri, 7 Dec 2012 00:58:01 +1100 Subject: Re: Confused compare function :) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354802285 news.xs4all.nl 6931 [2001:888:2000:d::a6]:33238 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34387 On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel wrote: > Am 06.12.2012 09:49 schrieb Bruno Dupuis: > >> The point is Exceptions are made for error handling, not for normal >> workflow. I hate when i read that for example: >> >> try: >> do_stuff(mydict[k]) >> except KeyError: >> pass > > I would do > > try: > value = mydict[k] > except KeyError: > pass > else: > do_stuff(k) > > Why? Because do_stuff() might raise a KeyError, which should not go > undetected. (Assuming first off that you meant "do_stuff(value)", not "do_stuff(k)", in that last line) That has quite different functionality, though. The original wouldn't have called do_stuff at all if k is not in dict, behaviour which is matched by both his EAFP and his LBLY. But your version, in the event of a KeyError, will call do_stuff with the previous value of value, or raise NameError if there is no such previous value. I don't think that's intentional. The only way around it that I can see is an extra condition or jump - something like: def call_if_present(mydict,k,do_stuff): """Equivalent to do_stuff(mydict[k]) if the key is present; otherwise, does not call do_stuff, and returns None.""" try: value = mydict[k] except KeyError: return return do_stuff(value) It'll propagate any other exceptions from the subscripting (eg TypeError if you give it a list instead of a dict), and any exceptions from do_stuff itself. But it's getting a bit unwieldy. ChrisA