Re: LBYL vs EAFP

2013-02-05 Thread Chris Angelico
On Tue, Feb 5, 2013 at 11:04 PM, Steven D'Aprano wrote: > py> isinstance(NAN, Number) > True Does that line of code count as nerd humour? ChrisA -- http://mail.python.org/mailman/listinfo/python-list

Re: LBYL vs EAFP

2013-02-05 Thread Steven D'Aprano
Pete Forman wrote: > Steven D'Aprano writes: >>> I want to check that a value is a number. [...] >> I'm leaning towards an isinstance check [...] > BTW what if the value is Not-a-Number? ;-) Nothing different, and hopefully exactly what the caller expects. As far as Python is concerned, NANs are

Re: LBYL vs EAFP

2013-02-05 Thread Pete Forman
Steven D'Aprano writes: >> I want to check that a value is a number. [...] > I'm leaning towards an isinstance check Well that is the answer to your question, whether the value *is* a number. EAFP can answer the question whether the value *behaves* like a number, where the criterion depends on wh

Re: LBYL vs EAFP

2013-02-04 Thread Terry Reedy
On 2/4/2013 6: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... A somewhat different answer is that it depends on what you want the function to do, as documented and *tested*. And th

Re: LBYL vs EAFP

2013-02-04 Thread Steven D'Aprano
On Tue, 05 Feb 2013 16:20:19 +1100, Chris Angelico wrote: > On Tue, Feb 5, 2013 at 3:52 PM, Steven D'Aprano > wrote: >> There's also the principle that it is best to raise an exception as >> early as possible. It's easier to track down errors at the point they >> are introduced than long afterwar

Re: LBYL vs EAFP

2013-02-04 Thread Ian Kelly
On Mon, Feb 4, 2013 at 9:52 PM, Steven D'Aprano wrote: > You seem to be making the > classic mistake of thinking that exceptions are something to avoid: Far from it. You've extrapolated a lot more than what I actually said, and I completely agree with everything you wrote. I was explaining EAFP

Re: LBYL vs EAFP

2013-02-04 Thread Chris Angelico
On Tue, Feb 5, 2013 at 3:52 PM, Steven D'Aprano wrote: > There's also the principle that it is best to raise an exception as early > as possible. It's easier to track down errors at the point they are > introduced than long afterwards. Yes, definitely, especially (as was mentioned) if you're work

Re: LBYL vs EAFP

2013-02-04 Thread Chris Angelico
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 o

Re: LBYL vs EAFP

2013-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2013 16:46:11 -0700, Ian Kelly wrote: > Presumably if the operation requires > a number, then it will at some point perform some kind of numerical > manipulation that will raise a TypeError if one is not passed. If the > operation succeeds, then the object supported all the operatio

Re: LBYL vs EAFP

2013-02-04 Thread Steven D'Aprano
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 l

Re: LBYL vs EAFP

2013-02-04 Thread Oscar Benjamin
On 4 February 2013 23:16, Steven D'Aprano wrote: > > 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

Re: LBYL vs EAFP

2013-02-04 Thread Ethan Furman
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

Re: LBYL vs EAFP

2013-02-04 Thread Chris Angelico
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 abou

Re: LBYL vs EAFP

2013-02-04 Thread Dave Angel
On 02/04/2013 06:38 PM, 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,

Re: LBYL vs EAFP

2013-02-04 Thread Ian Kelly
On Feb 4, 2013 4:24 PM, "Steven D'Aprano" < steve+comp.lang.pyt...@pearwood.info> 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 wha

Re: LBYL vs EAFP

2013-02-04 Thread Chris Angelico
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 ea