Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder7.xlned.com!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'exception': 0.03; 'caller': 0.07; 'suppose': 0.07; 'given,': 0.09; 'instance.': 0.09; 'rows': 0.09; "wouldn't": 0.11; 'library': 0.15; 'bug...': 0.16; 'elsewhere,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator': 0.16; 'number?': 0.16; 'uncommon': 0.16; 'unsafe': 0.16; 'wrote:': 0.17; 'examples': 0.18; 'saying': 0.18; '>>>': 0.18; 'feb': 0.19; 'sort': 0.21; 'all,': 0.21; 'trying': 0.21; 'addition,': 0.21; 'earlier': 0.21; 'tells': 0.22; 'seems': 0.23; 'idea': 0.24; 'testing': 0.24; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'checking': 0.27; 'prevent': 0.27; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'lines': 0.28; 'received:209.85.212': 0.28; 'chris': 0.28; 'along.': 0.29; "d'aprano": 0.29; 'forces': 0.29; 'secondly': 0.29; 'steven': 0.29; 'case,': 0.29; "i'm": 0.29; 'error': 0.30; 'code': 0.31; 'support,': 0.32; 'could': 0.32; 'maintained': 0.33; 'surely': 0.33; 'traceback': 0.33; 'turns': 0.33; 'to:addr:python-list': 0.33; 'operations': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'or,': 0.34; 'wrong': 0.34; 'third': 0.34; 'along': 0.35; 'fail': 0.35; 'massive': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'next': 0.35; 'but': 0.36; 'depends': 0.36; 'should': 0.36; 'itself': 0.37; 'option': 0.37; 'two': 0.37; 'being': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'comment': 0.38; 'object': 0.38; 'supports': 0.38; 'some': 0.38; 'sure': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'help': 0.40; 'your': 0.60; 'further': 0.61; 'side': 0.61; 'kind': 0.61; 'between': 0.63; 'making': 0.64; 'therefore': 0.65; 'taking': 0.65; 'treat': 0.65; 'middle': 0.66; '"oh,': 0.84; '2013': 0.84; 'blow': 0.84; 'float,': 0.84; 'miles.': 0.84; 'multiply': 0.84; 'dozen': 0.91; 'mean.': 0.91; 'that),': 0.91; 'average': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=8U69USJpMEt8FDziw4C/xgPXfvmJqgQcFdMCt2ZSFUs=; b=tt9fq50NnYfjBg68Ehfew5L7fIYhbwA8nxvaxPvscPkcflh6vNuM+QXJ/vg1PDAOIS kZ+OfMQGjy8TxjawXDi+84TNKDK2jyAFNhE26kaDCDPR8f+bKoOQcp5x4P1bKU7J/7qG bdo/03XyOnuBPqFyvuwL6Qe1qRSfAy3sG7hPN32st5/HEAvte/xLuVdJJhixk12PY6jr 9dhW19iR/onsMfAb7mq6T6EFREknyzSEqeK1gJKyzoE5zAGxTPoVNWlHw4u4C1N42xiO 6g1zT/vBnwgTW0DB9tJte+0k+7CigTOxPtuNpaRQVO+8ieQhKewJ8KMHGuWLNnjCb7Uq 4+PQ== MIME-Version: 1.0 X-Received: by 10.220.156.10 with SMTP id u10mr25801640vcw.28.1360041560325; Mon, 04 Feb 2013 21:19:20 -0800 (PST) In-Reply-To: <511081e3$0$21856$c3e8da3$76491128@news.astraweb.com> References: <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> <511081e3$0$21856$c3e8da3$76491128@news.astraweb.com> Date: Tue, 5 Feb 2013 16:19:20 +1100 Subject: Re: LBYL vs EAFP 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360041568 news.xs4all.nl 6977 [2001:888:2000:d::a6]:41444 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38162 On Tue, Feb 5, 2013 at 2:52 PM, Steven D'Aprano wrote: > On Tue, 05 Feb 2013 10:38:41 +1100, Chris Angelico wrote: > >> On Tue, Feb 5, 2013 at 10:16 AM, Steven D'Aprano >> wrote: >>> A third option is not to check x at all, and hope that it will blow up >>> at some arbitrary place in the middle of my code rather than silently >>> do the wrong thing. I don't like this idea because, even if it fails, >>> it is better to fail earlier than later. > [...] > >> With >> the specific examples given, I wouldn't like to use "x + 0" as a check; >> it seems dodgy. Firstly because it doesn't look like a data type check > > Strange. To me, it looks like "oh, you're testing whether x supports > numeric addition". I suppose the hidden assumption I'm making is that if > x supports addition, it is a kind of number. Perhaps that's an unsafe > assumption. If your code bombs and the exception traceback shows a line saying "x + 0", I'm going to wonder if it's a massive bug... or, worse, if you're expecting x.__add__() to have stupid side effects ("yeah, adding 0 to x turns x into a float, but subtracting 0 from it forces it to be an int"). >> (though a comment can help with that), and secondly because something >> might very well support having a number added to it while definitely not >> itself being a number - eg something along the lines of a database >> cursor. > > But surely duck-typing tells us that if database cursors support the same > sort of operations that numbers support, we should therefore treat them > as a kind of number? Depends what operations you mean. It's not uncommon for a cursor/iterator object to support "x+=1" to move to the next row/element/whatever; if your result set is maintained elsewhere, you could cheaply support "x + 4" to create a new iterator that's four rows further along. But in all other respects, it's not a number. You can't multiply an iterator by 2, for instance. > My library will not try to prevent the caller taking the average of (say) > a dozen eggs and 10 miles. I'm not sure that it should try to prevent the > caller from trying to take the average between two database cursors. Well, sure... if your code is so simple and trivial, and if the cursors support "x - y" to get the number of rows between them. But in that case, go for true EAFP and eschew error checking altogether. ChrisA