Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38151

Re: LBYL vs EAFP

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'case.': 0.05; 'kind,': 0.05; 'python': 0.09; '"if': 0.09; 'failure.': 0.09; 'given,': 0.09; 'programmer': 0.11; "wouldn't": 0.11; 'library': 0.15; 'cases': 0.15; 'between.': 0.16; 'caring': 0.16; 'efficiency.': 0.16; 'inputs': 0.16; 'lowercase': 0.16; 'operators.': 0.16; 'positive,': 0.16; 'specified.': 0.16; 'trace.': 0.16; 'wrote:': 0.17; 'tend': 0.17; 'examples': 0.18; 'feb': 0.19; 'define': 0.20; 'all,': 0.21; 'earlier': 0.21; 'assuming': 0.22; "i'd": 0.22; 'programming': 0.23; 'seems': 0.23; 'raise': 0.24; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'values': 0.26; 'am,': 0.27; 'checking': 0.27; 'question': 0.27; 'replace': 0.27; 'thoughts': 0.27; "doesn't": 0.28; 'lines': 0.28; 'chris': 0.28; "d'aprano": 0.29; 'description,': 0.29; 'secondly': 0.29; 'steven': 0.29; 'class': 0.29; 'this.': 0.29; "i'm": 0.29; 'classes': 0.30; 'error': 0.30; 'expect': 0.31; 'code': 0.31; 'url:python': 0.32; 'could': 0.32; 'defining': 0.33; 'int': 0.33; 'values.': 0.33; 'zero': 0.33; 'to:addr:python-list': 0.33; 'point.': 0.33; 'agree': 0.34; 'wrong': 0.34; 'third': 0.34; 'along': 0.35; 'fail': 0.35; 'so,': 0.35; 'pm,': 0.35; 'something': 0.35; 'next': 0.35; 'but': 0.36; 'url:org': 0.36; 'depends': 0.36; 'url:library': 0.36; "didn't": 0.36; 'test': 0.36; 'should': 0.36; 'itself': 0.37; 'option': 0.37; 'two': 0.37; 'being': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'comment': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'real': 0.61; 'situation': 0.62; 'skip:n 10': 0.63; 'middle': 0.66; 'user,': 0.69; 'obvious': 0.71; 'received:74.208': 0.71; 'opinions': 0.72; '"what': 0.84; '2013': 0.84; 'asked.': 0.84; 'blow': 0.84; 'received:74.208.4.194': 0.84; 'abc': 0.91; 'that),': 0.91; 'anywhere,': 0.93
Date Mon, 04 Feb 2013 18:55:05 -0500
From Dave Angel <davea@davea.name>
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> <CAPTjJmrvvjmvkT+-4DTpk5qyPg5FsuYKcesbj4tPt8XhJoKi6g@mail.gmail.com>
In-Reply-To <CAPTjJmrvvjmvkT+-4DTpk5qyPg5FsuYKcesbj4tPt8XhJoKi6g@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:lehmDTaoAcy0hys08UUbF+cA9eq5WuNI5+Ihw+98hA4 Zu/jJFLVw+TCooNsOHYeZYJrmOqHctqK9iJCRgB+qS15YCPgwJ XXAI84Lz9GOUYYDW54EHNKOY6FgJ3f1WDQoS9Gl/H6uEUPxz6X Yl7hc01PAngJQlLPckMn/UAyPhvlrpgg/kQUP+0sYb0S/pg5YG A5/+2/K0KWpoaIeDbh8vrLoGzAnk6Fq2v/7WBqBCw1cfpwOazs ITfhkRBup7027+ZXPk8ToZXkAeR2FrE8NjiKSPOI2x4N/MycJw t0SD0Iz9Gfh+mWT0ZVlySZFdeBEHxA5WrIDAAUJzt9jmW8JrA= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.1341.1360022138.2939.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360022138 news.xs4all.nl 6979 [2001:888:2000:d::a6]:36941
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38151

Show key headers only | View raw


On 02/04/2013 06:38 PM, Chris Angelico wrote:
> On Tue, Feb 5, 2013 at 10:16 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> 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.
>>
>> Comments, thoughts and opinions please.
>
> It depends on what could cause the failure. If only a programming
> error could cause x to not be a number, I'd go with your third option
> - let it blow up anywhere, and follow the trace. That option requires
> zero forethought, which translates directly into programmer
> efficiency. But if x came from a user,  then I'd be checking inputs at
> a much earlier point.
>
> Those are the two obvious cases though, and I'm assuming your
> situation is neither of them. Writing library code is half way in
> between. 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 (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. So I would recommend LBYL for this
> particular case.
>
> ChrisA
>

I agree with Chris.  It would seem that the main purpose of the abc 
numbers.Number is for this.  See the page:

http://docs.python.org/2/library/numbers.html
   "If you just want to check if an argument x is a number, without 
caring what kind, use isinstance(x, Number)."

But the real question is "what is a number?" .   You used lowercase in 
your description, so you weren't defining it as the particular classes 
that were already associated with Number.  I expect you're using Python 
3, which you should have specified.  If so, I'd tend to replace the x+0 
with x > 0    which would be an error for any class other than int which 
didn't define the corresponding dunder operators.


Coincidentally, that's the next question you asked.  So if you're only 
interested in values that are positive, that would seem to be clearer.

Unfortunately, that test would raise a TypeError for complex values.



-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 10:16 +1100
  Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 10:38 +1100
    Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 03:52 +0000
      Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 16:19 +1100
  Re: LBYL vs EAFP Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-04 16:46 -0700
    Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 04:52 +0000
      Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 16:20 +1100
        Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 06:31 +0000
          Re: LBYL vs EAFP Pete Forman <petef4+usenet@gmail.com> - 2013-02-05 09:49 +0000
            Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 23:04 +1100
              Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 23:25 +1100
      Re: LBYL vs EAFP Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-04 22:40 -0700
  Re: LBYL vs EAFP Dave Angel <davea@davea.name> - 2013-02-04 18:55 -0500
  Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 11:45 +1100
  Re: LBYL vs EAFP Ethan Furman <ethan@stoneleaf.us> - 2013-02-04 16:26 -0800
  Re: LBYL vs EAFP Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-05 01:00 +0000
  Re: LBYL vs EAFP Terry Reedy <tjreedy@udel.edu> - 2013-02-05 02:53 -0500

csiph-web