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


Groups > comp.lang.python > #38151

Re: LBYL vs EAFP

Date 2013-02-04 18:55 -0500
From Dave Angel <davea@davea.name>
Subject Re: LBYL vs EAFP
References <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmrvvjmvkT+-4DTpk5qyPg5FsuYKcesbj4tPt8XhJoKi6g@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1341.1360022138.2939.python-list@python.org> (permalink)

Show all headers | 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