Re: How to test if object is an integer?

2011-10-17 Thread Steven D'Aprano
On Mon, 17 Oct 2011 18:59:44 -0600, Ian Kelly wrote:

> On Mon, Oct 17, 2011 at 6:40 PM, Chris Kaynor 
> wrote:
>> Python 2.6 running on Windows 7:
> 99.0**99**99
>> OverflowError: (34, 'Result too large') Traceback (most recent call
>> last):
>>   File "", line 1, in 
>> OverflowError: (34, 'Result too large')
>>
>> However, from the documentation:
>> "Because of the lack of standardization of floating point exception
>> handling in C, most floating point operations also aren’t checked."
>> (http://docs.python.org/library/
exceptions.html#exceptions.OverflowError)
> 
> I think what Roy meant was "can you even get an OverflowError from
> calling int() any more", to which I think the answer is no, since in
> modern Pythons int() will auto-promote to a long, and in Python 3
> they're even the same thing.


You can still get an OverflowError:

>>> inf = float('inf')
>>> int(inf)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: cannot convert float infinity to integer


and similarly for Decimal('inf') as well.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Yingjie Lan




- Original Message -
> From: Noah Hall 
> To: MrPink 
> Cc: python-list@python.org
> Sent: Tuesday, October 18, 2011 4:44 AM
> Subject: Re: How to test if object is an integer?

> There's the isdigit method, for example -
> 
>>>>  str = "1324325"
>>>>  str.isdigit()
> True
>>>>  str = "1232.34"
>>>>  str.isdigit()
> False
>>>>  str = "I am a string, not an int!"
>>>>  str.isdigit()
> False
>

There are some corner cases to be considered with this approach:
1. negative integers: '-3'
2. strings starting with '0': '03'
3. strings starting with one '+': '+3'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Ian Kelly
On Mon, Oct 17, 2011 at 6:40 PM, Chris Kaynor  wrote:
> Python 2.6 running on Windows 7:
 99.0**99**99
> OverflowError: (34, 'Result too large')
> Traceback (most recent call last):
>   File "", line 1, in 
> OverflowError: (34, 'Result too large')
>
> However, from the documentation:
> "Because of the lack of standardization of floating point exception
> handling in C, most floating point operations also aren’t checked."
> (http://docs.python.org/library/exceptions.html#exceptions.OverflowError)

I think what Roy meant was "can you even get an OverflowError from
calling int() any more", to which I think the answer is no, since in
modern Pythons int() will auto-promote to a long, and in Python 3
they're even the same thing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Chris Kaynor
Python 2.6 running on Windows 7:
>>> 99.0**99**99
OverflowError: (34, 'Result too large')
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: (34, 'Result too large')

However, from the documentation:
"Because of the lack of standardization of floating point exception
handling in C, most floating point operations also aren’t checked."
(http://docs.python.org/library/exceptions.html#exceptions.OverflowError)

Chris

On Mon, Oct 17, 2011 at 5:33 PM, Roy Smith  wrote:
>
> In article ,
>  Mathias Lafeldt  wrote:
>
> > According to [1], there're more Exceptions to test for:
> >
> > try:
> >     int(s)
> >     return True
> > except (TypeError, ValueError, OverflowError): # int conversion failed
> >     return False
>
>
> I don't think I would catch TypeError here.  It kind of depends on how
> isInt() is defined.  Is it:
>
> def isInt(s):
>  "Return True if s is a string representing an integer"
>
> or is it:
>
> def isInt(s):
>  "Return True if s (which must be a string) represents an integer"
>
> If the latter, then passing a non-string violates the contract, and the
> function should raise TypeError.  If the former, then you could make
> some argument for catching the TypeError and returning False, but I
> think the second version is what most people have in mind for isInt().
>
> Can you even get an OverflowError any more in a modern Python?
>
> >>>
> int('9')
> 9L
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Roy Smith
In article ,
 Mathias Lafeldt  wrote:

> According to [1], there're more Exceptions to test for:
> 
> try:
> int(s)
> return True
> except (TypeError, ValueError, OverflowError): # int conversion failed
> return False


I don't think I would catch TypeError here.  It kind of depends on how 
isInt() is defined.  Is it:

def isInt(s):
  "Return True if s is a string representing an integer"

or is it:

def isInt(s):
  "Return True if s (which must be a string) represents an integer"

If the latter, then passing a non-string violates the contract, and the 
function should raise TypeError.  If the former, then you could make 
some argument for catching the TypeError and returning False, but I 
think the second version is what most people have in mind for isInt().

Can you even get an OverflowError any more in a modern Python?

>>> 
int('9')
9L
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Ian Kelly
On Mon, Oct 17, 2011 at 2:44 PM, Noah Hall  wrote:
> There's the isdigit method, for example -
>
 str = "1324325"
 str.isdigit()
> True
 str = "1232.34"
 str.isdigit()
> False
 str = "I am a string, not an int!"
 str.isdigit()
> False

That works for non-negative base-10 integers.  But:

>>> "-1234".isdigit()
False

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Noah Hall
On Sat, Oct 15, 2011 at 12:44 AM, MrPink  wrote:
>
> Is there a function in Python that can be used to test if the value in
> a string is an integer?  I had to make one up for myself and it looks
> like this:
>
> def isInt(s):
>    try:
>        i = int(s)
>        return True
>    except ValueError:
>        return False


There's the isdigit method, for example -

>>> str = "1324325"
>>> str.isdigit()
True
>>> str = "1232.34"
>>> str.isdigit()
False
>>> str = "I am a string, not an int!"
>>> str.isdigit()
False
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-17 Thread Mathias Lafeldt
On Sat, Oct 15, 2011 at 1:44 AM, MrPink  wrote:
>
> Is there a function in Python that can be used to test if the value in
> a string is an integer?  I had to make one up for myself and it looks
> like this:
>
> def isInt(s):
>    try:
>        i = int(s)
>        return True
>    except ValueError:
>        return False

According to [1], there're more Exceptions to test for:

try:
int(s)
return True
except (TypeError, ValueError, OverflowError): # int conversion failed
return False

[1] http://jaynes.colorado.edu/PythonIdioms.html, idiom "Catch errors
rather than avoiding them to avoid cluttering your code with special
cases"

-Mathias
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Terry Reedy

On 10/14/2011 9:51 PM, Ben Finney wrote:

Terry Reedy  writes:


On 10/14/2011 9:05 PM, Chris Angelico wrote:



That tests if the object is already an int; the OP asked if a string
contains an integer.


The misleading subject line did not. It should have been "How to test
if a string contains an integer?"



Which would still be misleading :-)

Even better is “How to test whether a string is a valid representation
of an integer?”


I agree, but that is more than I would ask of a newbie, whereas asking 
people to ask the same question in subject line and text, even if the 
question is inadequate, is reasonable.



I say that's better because it gets to the relevant point of asking
*which* representations you want to test for – what qualifies as valid
for your particular use case, and what does not. There's no single right
answer; the programmer must choose exactly what they want to test for.


Yes. Even the wrong subject line question is ambiguous, as any of int, 
bool, float, complex, decimal.Decimal, and fractions.Fraction can have 
an integer value, as might user class instances, and, of course, 
depending on context, bytes and strings.


--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Ben Finney
Terry Reedy  writes:

> On 10/14/2011 9:05 PM, Chris Angelico wrote:

> > That tests if the object is already an int; the OP asked if a string
> > contains an integer.
>
> The misleading subject line did not. It should have been "How to test
> if a string contains an integer?"

Which would still be misleading :-)

Even better is “How to test whether a string is a valid representation
of an integer?”

I say that's better because it gets to the relevant point of asking
*which* representations you want to test for – what qualifies as valid
for your particular use case, and what does not. There's no single right
answer; the programmer must choose exactly what they want to test for.

-- 
 \  “When I was a little kid we had a sand box. It was a quicksand |
  `\   box. I was an only child... eventually.” —Steven Wright |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Terry Reedy

On 10/14/2011 9:05 PM, Chris Angelico wrote:

2011/10/15 惜悯:

retrun True if type(i) is int else False


That tests if the object is already an int; the OP asked if a string
contains an integer.


The misleading subject line did not. It should have been
"How to test if a string contains an integer?"

--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Chris Rebert
On Fri, Oct 14, 2011 at 6:05 PM, Chris Angelico  wrote:
> 2011/10/15 惜悯 :
>> retrun True if type(i) is int else False
>
> That tests if the object is already an int; the OP asked if a string
> contains an integer.

Additionally:
* the if-then-else there is unnecessary since `type(i) is int` already
returns a bool
* such a type test is normally and better written `isinstance(i, int)`

Cheers,
Chris R.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Chris Angelico
2011/10/15 惜悯 :
> retrun True if type(i) is int else False

That tests if the object is already an int; the OP asked if a string
contains an integer.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread 惜悯
retrun True if type(i) is int else False
 
 
-- Original --
From:  "Chris Angelico";
Date:  Sat, Oct 15, 2011 08:55 AM
To:  "python-list"; 

Subject:  Re: How to test if object is an integer?

 
On Sat, Oct 15, 2011 at 10:44 AM, MrPink  wrote:
> Is there a function in Python that can be used to test if the value in
> a string is an integer?  I had to make one up for myself and it looks
> like this:
>
> def isInt(s):
>try:
>i = int(s)
>return True
>except ValueError:
>return False

There's some ambiguity in the definition of "is an integer". For
instance, is "0x100" an integer? Is "0800"? If your definition of "is
an integer" is "can be passed to int() without triggering an
exception" (which is probably the most useful), then your above code
is about perfect. The only change I'd make is to not have an isInt
function at all, but simply to try/except at the point where you need
to make the conversion.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is an integer?

2011-10-14 Thread Chris Angelico
On Sat, Oct 15, 2011 at 10:44 AM, MrPink  wrote:
> Is there a function in Python that can be used to test if the value in
> a string is an integer?  I had to make one up for myself and it looks
> like this:
>
> def isInt(s):
>    try:
>        i = int(s)
>        return True
>    except ValueError:
>        return False

There's some ambiguity in the definition of "is an integer". For
instance, is "0x100" an integer? Is "0800"? If your definition of "is
an integer" is "can be passed to int() without triggering an
exception" (which is probably the most useful), then your above code
is about perfect. The only change I'd make is to not have an isInt
function at all, but simply to try/except at the point where you need
to make the conversion.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


How to test if object is an integer?

2011-10-14 Thread MrPink

Is there a function in Python that can be used to test if the value in
a string is an integer?  I had to make one up for myself and it looks
like this:

def isInt(s):
try:
i = int(s)
return True
except ValueError:
return False
-- 
http://mail.python.org/mailman/listinfo/python-list