Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'puts': 0.07; 'check.': 0.09; 'sake': 0.09; 'cases': 0.15; 'caveat': 0.16; 'ellipsis': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'indent': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'feb': 0.19; 'import': 0.21; "i'd": 0.22; 'raise': 0.24; 'header :In-Reply-To:1': 0.25; 'am,': 0.27; 'handling': 0.27; 'message- id:@mail.gmail.com': 0.27; 'about.': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'previous': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'further': 0.61; 'information': 0.63; 'more': 0.63; '2013': 0.84; 'conditions,': 0.84; 'number):': 0.84 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=fl5n1uegVxjsKTGb70Y5qtYQQ9jgSCLnHErU2BfkRFY=; b=wkKR3nSD+XBJFqFr+ScaTr2fJ54e29b+CQb1gv4RdEZMxrPzfxoiDNt5JkBF8ffg/w FzLL8jqb+YSqXHIktshPa6HOmX5Yt9zt/lRgIQerKx3XpejJLMG5UShCtU1865rdJBAH 0762tpPymPoKxgeNC20ayjQaG/V5DszrJoaEBcmBdDxJDb7EPcqTX5TyhzIR6rKoGy7K NDuzYY7IIcErHnfOR/WYJALixeXszfBZvo/XeLC1qTenEUseG05ndU8snNSIxsYWz/CQ 0BFqeB6oBy6B92fMrxmoQMoqGZSGh3GUBTvibnyI0dyFVI5THejWAh7GA7UUzAM9hXcp SJjg== MIME-Version: 1.0 X-Received: by 10.224.31.205 with SMTP id z13mr19094191qac.76.1360025118334; Mon, 04 Feb 2013 16:45:18 -0800 (PST) In-Reply-To: <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 5 Feb 2013 11:45:18 +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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360025121 news.xs4all.nl 6842 [2001:888:2000:d::a6]:51474 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38153 On Tue, Feb 5, 2013 at 10:16 AM, Steven D'Aprano wrote: > from numbers import Number > if isinstance(x, Number): > ... > else: > raise TypeError > > > or Ask Forgiveness: > > x + 0 > ... > > > where in both cases the ellipsis ... is the code I actually care about. Caveat to my previous post: I would NOT indent the function body for the sake of this check. I'd negate it: if not isinstance(x, Number): raise TypeError (hopefully with further information in the TypeError). I don't like the code style that puts conditions, then more code, then error handling - I prefer to fail-and-bail. ChrisA