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
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
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
>:-)
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
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
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
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
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
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:
>
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
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
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
> > 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
>
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
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
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):
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
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
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
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
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 /
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
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:
*
> 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
"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
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:
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
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
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
"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
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
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
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
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):
> ...
>
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
35 matches
Mail list logo