Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34394
| 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 | <d@davea.name> |
| 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 <d@davea.name> |
| 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 <rosuav@gmail.com> |
| Subject | Re: Confused compare function :) |
| References | <CAKhY55PXaMkdnW7qMb-5ConHMcz05Gnkqw4wvyoprBFBd5UFFw@mail.gmail.com> <mailman.538.1354753193.29569.python-list@python.org> <k9p32p$nek$1@dont-email.me> <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <mailman.549.1354783761.29569.python-list@python.org> <k9q6s4$os9$1@r03.glglgl.gl> <CAPTjJmqhY2Nxcf-MqCZ2Z8EXvfAtDASt_oPVg+=WcwVFwBBqEQ@mail.gmail.com> |
| In-Reply-To | <CAPTjJmqhY2Nxcf-MqCZ2Z8EXvfAtDASt_oPVg+=WcwVFwBBqEQ@mail.gmail.com> |
| 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 <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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.563.1354804876.29569.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 12/06/2012 08:58 AM, Chris Angelico wrote: > On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel > <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> > 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 01:19 +0100
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:42 +0000
Re: Confused compare function :) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-06 13:41 -0500
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:55 +0100
Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 03:22 +0000
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 04:32 +0000
Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 09:49 +0100
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 11:47 +0000
Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 08:55 -0300
Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 14:32 +0100
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:47 +1100
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 23:14 +1100
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 22:16 +0000
Re: Confused compare function :) Terry Reedy <tjreedy@udel.edu> - 2012-12-08 02:01 -0500
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-08 18:17 +1100
Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-08 17:50 +0000
Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-09 14:22 +1100
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-09 07:39 +0000
Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-06 13:51 +0000
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 02:55 +0000
Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-07 16:40 +0000
Re: Confused compare function :) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-12-06 14:33 +0100
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:58 +1100
Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 15:21 +0100
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 01:28 +1100
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 15:22 +0100
Re: Confused compare function :) Dave Angel <d@davea.name> - 2012-12-06 09:40 -0500
Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 11:46 -0300
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 17:16 +0100
Re: Confused compare function :) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 16:52 +0000
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:08 +0100
Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:10 +0000
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:31 +0100
Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:52 +0000
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:25 +0100
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-07 14:36 +0100
Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:24 +0000
Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 20:34 +1100
Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:25 +0000
csiph-web