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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'exception': 0.03; 'value,': 0.03; 'clause': 0.07; 'try:': 0.07; 'handling,': 0.09; 'matched': 0.09; 'propagate': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'itself.': 0.11; "wouldn't": 0.11; 'dec': 0.15; 'value.': 0.15; '(assuming': 0.16; 'line)': 0.16; 'nameerror': 0.16; 'present;': 0.16; 'subject:compare': 0.16; 'subscripting': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'otherwise,': 0.20; 'bit': 0.21; 'meant': 0.21; 'exceptions': 0.22; 'keyerror:': 0.22; 'occurs': 0.22; 'cc:2**0': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'chris': 0.28; 'behaviour': 0.29; 'though.': 0.29; 'fri,': 0.30; 'version,': 0.30; 'error': 0.30; 'point': 0.31; 'getting': 0.33; 'like:': 0.33; 'list': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'execute': 0.37; 'does': 0.37; 'previous': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'received:192': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'thomas': 0.62; 'different': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'dict,': 0.84; 'rachel': 0.84; 'why?': 0.84; 'hate': 0.93 Date: Thu, 06 Dec 2012 09:40:51 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Chris Angelico Subject: Re: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:fM9WZJxVL/3DLrCHREDPFO4kEi/sKpiSGysM3G3E6OQ LNDLdB28HlG+ucEMcwX/3pC8FrSszLmWs7QEVGAd1KEXBUH9up JSOdvfyDiJg03D2azVk4oo9i536drgjUx86xW9QgVDgqEZRWwC 3YD41JYT5+yfglMrlCR1db8ZOER4PIrXTHJ/YX46a2URqtVSKq U4PFEp4bD8cx9ehlD6n1lgd5/BumrHb6tDfJWDACsT0W/NRoXz /ZLgXBZ0NP+zu59fO16rvf9twE2vWIlJYzdE4m/NpBLiRtbKtv QHcHVoaialgbL9iTCy3eEYuPrk8H0SuPdR9yCzHPTazMjynWA= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354804876 news.xs4all.nl 6940 [2001:888:2000:d::a6]:38717 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34394 On 12/06/2012 08:58 AM, Chris Angelico wrote: > 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. Nope. The else clause will only execute if no exception occurs in the value= line. > 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 -- DaveA