How to test if object is an integer?

Chris Kaynor ckaynor at zindagigames.com
Mon Oct 17 20:40:00 EDT 2011


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)

Chris

On Mon, Oct 17, 2011 at 5:33 PM, Roy Smith <roy at panix.com> wrote:
>
> In article <mailman.2051.1318881724.27778.python-list at python.org>,
>  Mathias Lafeldt <mathias.lafeldt at googlemail.com> 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('99999999999999999999999999999999999999999999999999999999999999999')
> 99999999999999999999999999999999999999999999999999999999999999999L
> --
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list