On Sat, Oct 15, 2011 at 1:44 AM, MrPink <[email protected]> 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