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; 'messages.': 0.04; 'mentioned,': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'cases': 0.15; 'eternal': 0.16; 'forgiveness': 0.16; 'friendlier': 0.16; 'personally,': 0.16; 'wrote:': 0.17; 'mathematical': 0.17; 'sort': 0.21; 'trying': 0.21; 'import': 0.21; 'int,': 0.22; 'example': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; "d'aprano": 0.29; 'steven': 0.29; "i'm": 0.29; 'error': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'pm,': 0.35; 'something': 0.35; 'should': 0.36; 'charset:us-ascii': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'further': 0.61; 'between': 0.63; 'received:69.56': 0.65; '"look': 0.84; 'complex,': 0.84; 'float,': 0.84; 'number):': 0.84 Date: Mon, 04 Feb 2013 16:26:37 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: LBYL vs EAFP References: <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: yes X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.235]) [173.12.184.235]:46832 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360025565 news.xs4all.nl 6850 [2001:888:2000:d::a6]:55051 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38154 On 02/04/2013 03:16 PM, Steven D'Aprano wrote: > The eternal conflict between "Look Before You Leap" and "Easier to Ask for > Forgiveness than Permission" (LBYL vs EAFP) continues... > > I want to check that a value is a number. Let's say I don't care what sort > of number -- float, int, complex, Fraction, Decimal, something else -- just > that it is a number. Should I: > > Look Before I Leap: > > from numbers import Number > if isinstance(x, Number): > ... > else: > raise TypeError > > > or Ask Forgiveness: > > x + 0 > ... As Ian mentioned, both cases are LYBL, unless of course your addition was just an example of some mathematical code you have further down. Personally, I go with EAFP unless I'm trying to present friendlier error messages. ~Ethan~