How to test if object is an integer?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Oct 17 23:56:57 EDT 2011


On Mon, 17 Oct 2011 18:59:44 -0600, Ian Kelly wrote:

> On Mon, Oct 17, 2011 at 6:40 PM, Chris Kaynor <ckaynor at zindagigames.com>
> wrote:
>> Python 2.6 running on Windows 7:
>>>>> 99.0**99**99
>> OverflowError: (34, 'Result too large') Traceback (most recent call
>> last):
>>   File "<stdin-inspect>", line 1, in <module>
>> 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 "<stdin>", line 1, in <module>
OverflowError: cannot convert float infinity to integer


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


-- 
Steven



More information about the Python-list mailing list