Re: check if object is number

2005-02-17 Thread Steven Bethard
Michael Spencer wrote: Christos TZOTZIOY Georgiou wrote: On Sat, 12 Feb 2005 16:01:26 -0800, rumours say that Michael Spencer <[EMAIL PROTECTED]> might have written: OK - then my entry is: assert obj+1 >= 1 :-) So -1 is not a number. At least not a legal one for Steven's function as I understoo

Re: check if object is number

2005-02-17 Thread Michael Spencer
Christos TZOTZIOY Georgiou wrote: On Sat, 12 Feb 2005 16:01:26 -0800, rumours say that Michael Spencer <[EMAIL PROTECTED]> might have written: Yup, that's basically what I'm doing right now. The question was really how to define that adapter function. =) Steve OK - then my entry is: ass

Re: check if object is number

2005-02-17 Thread TZOTZIOY
On Sat, 12 Feb 2005 16:01:26 -0800, rumours say that Michael Spencer <[EMAIL PROTECTED]> might have written: >> Yup, that's basically what I'm doing right now. The question was really >> how to define that adapter function. =) >> >> Steve >OK - then my entry is: > assert obj+1 >= 1 >:-)

Re: check if object is number

2005-02-16 Thread Steven Bethard
Fredrik Lundh wrote: Steven Bethard wrote: Actually no, floats don't meet this behaviour or more specifically floats don't guarantee this behaviour. It depends of course on your implementation of f, but it is possible with floats to keep incrementing and never reach a maximum. My code won't hit thi

Re: check if object is number

2005-02-16 Thread Fredrik Lundh
Steven Bethard wrote: >> Actually no, floats don't meet this behaviour or more specifically >> floats don't guarantee this behaviour. It depends of course on >> your implementation of f, but it is possible with floats to keep >> incrementing and never reach a maximum. > > My code won't hit this co

Re: check if object is number

2005-02-15 Thread Steven Bethard
Antoon Pardon wrote: Op 2005-02-11, Steven Bethard schreef <[EMAIL PROTECTED]>: George Sakkis wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Is there a good way to determine if an object is a numeric type? In your example, what does your application consider to

Re: check if object is number

2005-02-15 Thread Antoon Pardon
Op 2005-02-11, Steven Bethard schreef <[EMAIL PROTECTED]>: > George Sakkis wrote: >> "Steven Bethard" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >> >>>Is there a good way to determine if an object is a numeric type? >> >> In your example, what does your application consider to

Re: check if object is number

2005-02-14 Thread Mark English
Not sure if anyone's mentioned this yet, but just in case they haven't: Start bit o' Python >>> import operator >>> operator.isNumberType(1) True >>> operator.isNumberType(1.01) True >>> operator.isNumberType('a') False >>> operator.isNumberType('1') False End bit o' Python Have

Re: check if object is number

2005-02-13 Thread Francis Girard
Le vendredi 11 Février 2005 20:11, Steven Bethard a écrit : > Is there a good way to determine if an object is a numeric type? > Generally, I avoid type-checks in favor of try/except blocks, but I'm > not sure what to do in this case: > > def f(i): > ... > if x < i: >

Re: check if object is number

2005-02-12 Thread Michael Spencer
Steven Bethard wrote: Michael Spencer wrote: Steven Bethard wrote: Peter Hansen wrote: Of course, most of the other definitions of "is a number" that have been posted may likewise fail (defined as not doing what the OP would have wanted, in this case) with a numarray arange. How about explicitly c

Re: check if object is number

2005-02-12 Thread Steven Bethard
Michael Spencer wrote: Steven Bethard wrote: Peter Hansen wrote: Of course, most of the other definitions of "is a number" that have been posted may likewise fail (defined as not doing what the OP would have wanted, in this case) with a numarray arange. How about explicitly calling an adapter in yo

Re: check if object is number

2005-02-12 Thread Michael Spencer
Steven Bethard wrote: Peter Hansen wrote: Of course, most of the other definitions of "is a number" that have been posted may likewise fail (defined as not doing what the OP would have wanted, in this case) with a numarray arange. Or maybe not. (Pretty much all of them will call an arange a number

Re: check if object is number

2005-02-12 Thread George Sakkis
> > So what you're saying is that 3 <= "3.0" should not be allowed, but > > 3 <= SomeUserDefinedNumericClass(3) is ok, although your program knows > > nothing a priori about SomeUserDefinedNumericClass. The idea suggested > > before, try if "x+1" fails or not does not get you far; any class that >

Re: check if object is number

2005-02-12 Thread John Lenton
On Fri, Feb 11, 2005 at 01:17:55PM -0700, Steven Bethard wrote: > George Sakkis wrote: > >"Steven Bethard" <[EMAIL PROTECTED]> wrote in message > >news:[EMAIL PROTECTED] > > > >>Is there a good way to determine if an object is a numeric type? > > > >In your example, what does your application consi

Re: check if object is number

2005-02-12 Thread Michael Hartl
Oops, my bad. The utilities file I use gets loaded automatically when I start my interpreter, so I mistook isnumber for a built-in function. A battle-tested isnumber function is defined in Peter Norvig's utils.py (http://aima.cs.berkeley.edu/python/utils.py): #def isnumber(x): #"Is x a number

Re: check if object is number

2005-02-12 Thread Steven Bethard
George Sakkis wrote: George Sakkis wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Is there a good way to determine if an object is a numeric type? In your example, what does your application consider to be numeric? Well, here's the basic code: def f(max=None):

Re: check if object is number

2005-02-12 Thread Steven Bethard
John Lenton wrote: On Fri, Feb 11, 2005 at 01:17:55PM -0700, Steven Bethard wrote: George Sakkis wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Is there a good way to determine if an object is a numeric type? In your example, what does your application consider

Re: check if object is number

2005-02-12 Thread Steven Bethard
Fredrik Lundh wrote: Steven Bethard wrote: Is there a good way to determine if an object is a numeric type? assert operator.isNumberType(i) Interesting, thanks! If I read the source right, PyNumber_Check (which operator.isNumberType is an alias for) basically just returns True if the object's ty

Re: check if object is number

2005-02-12 Thread Steven Bethard
Peter Hansen wrote: Of course, most of the other definitions of "is a number" that have been posted may likewise fail (defined as not doing what the OP would have wanted, in this case) with a numarray arange. Or maybe not. (Pretty much all of them will call an arange a number... would the OP's fun

Re: check if object is number

2005-02-12 Thread Peter Hansen
marco wrote: Steven Bethard wrote: Is there a good way to determine if an object is a numeric type? Maybe this can help? def isnumber(x): try: return(x == x-0) except: return False Not exactly foolproof: >>> def isnumber(x): ... try: return (x == x-0) ... except: return

Re: check if object is number

2005-02-12 Thread marco
Steven Bethard wrote: Is there a good way to determine if an object is a numeric type? . . . Ideas? Maybe this can help? def isnumber(x): try: return(x == x-0) except: return False print '1:\t', isnumber(1) print '1.25:\t', isnumber(1.25) print '1.0 / 7:\t', isnumber(1.0 /

Re: check if object is number

2005-02-12 Thread Michael Hartl
As I mention below, I mistook the function from my utilities file for a Python built-in; here's the implementation: #def isnumber(x): #"Is x a number? We say it is if it has an __int__ method." #return hasattr(x, '__int__') -- http://mail.python.org/mailman/listinfo/python-list

Re: check if object is number

2005-02-12 Thread Steven Bethard
George Sakkis wrote: For the record, here's the arbitrary and undocumented (?) order among some main builtin types: None < 0 == 0.0 < {} < [] < "" < () If you're curious, you can check the source code. Look for default_3way_compare in object.c. Basically the rundown for dissimilar types is: *

Re: check if object is number

2005-02-11 Thread George Sakkis
> George Sakkis wrote: > > "Steven Bethard" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > > > >>Is there a good way to determine if an object is a numeric type? > > > > In your example, what does your application consider to be numeric? > > Well, here's the basic code: > > def f

Re: check if object is number

2005-02-11 Thread George Sakkis
"Michael Hartl" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > As luck would have it, isnumber is a built-in Python function: > > >>> isnumber(1) > True > >>> isnumber(1.0) > True > >>> isnumber('1') > False > > Michael > > -- > Michael D. Hartl, Ph.D. > Chief Technology Officer > h

Re: check if object is number

2005-02-11 Thread Fredrik Lundh
Michael Hartl wrote > As luck would have it, isnumber is a built-in Python function: > isnumber(1) > True isnumber(1.0) > True isnumber('1') > False and what strange Python version would that be? >>> isnumber(1) Traceback (most recent call last): File "", line 1, in ? NameError:

Re: check if object is number

2005-02-11 Thread Michael Hartl
As luck would have it, isnumber is a built-in Python function: >>> isnumber(1) True >>> isnumber(1.0) True >>> isnumber('1') False Michael -- Michael D. Hartl, Ph.D. Chief Technology Officer http://quarksports.com/ -- http://mail.python.org/mailman/listinfo/python-list

Re: check if object is number

2005-02-11 Thread Fredrik Lundh
Steven Bethard wrote: > Is there a good way to determine if an object is a numeric type? Generally, I > avoid type-checks in > favor of try/except blocks, but I'm not sure what to do in this case: > > def f(i): > ... > if x < i: > ... > > The problem is, no error

Re: check if object is number

2005-02-11 Thread Steven Bethard
George Sakkis wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Is there a good way to determine if an object is a numeric type? In your example, what does your application consider to be numeric? Well, here's the basic code: def f(max=None): ... while max

Re: check if object is number

2005-02-11 Thread George Sakkis
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Is there a good way to determine if an object is a numeric type? > Generally, I avoid type-checks in favor of try/except blocks, but I'm > not sure what to do in this case: > > def f(i): > ... > i

Re: check if object is number

2005-02-11 Thread Steven Bethard
Dan Bishop wrote: Steven Bethard wrote: Is there a good way to determine if an object is a numeric type? How about this? ... def is_number(x): ...try: ... x + 1 ... return True ...except TypeError: ... return False Great, thanks! That's the kind of thing I was looking for

Re: check if object is number

2005-02-11 Thread Steven Bethard
Bill Mill wrote: On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: Is there a good way to determine if an object is a numeric type? How about: if type(variable) == type(1): print "is an integer" else: print "please input an integer" This checks if it is an integ

Re: check if object is number

2005-02-11 Thread Dan Bishop
Steven Bethard wrote: > Is there a good way to determine if an object is a numeric type? > Generally, I avoid type-checks in favor of try/except blocks, but I'm > not sure what to do in this case: > > def f(i): > ... > if x < i: > ... > > The problem is, no erro

Re: check if object is number

2005-02-11 Thread Bill Mill
On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: > Is there a good way to determine if an object is a numeric type? > Generally, I avoid type-checks in favor of try/except blocks, but I'm > not sure what to do in this case: > > def f(i): > ... >

check if object is number

2005-02-11 Thread Steven Bethard
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i: ... The problem is, no error will be thrown if 'i' is, say, a string: p