How to check if a string "is" an int?

Dave Hansen iddw at hotmail.com
Wed Dec 21 18:17:16 EST 2005


On 21 Dec 2005 14:36:32 -0800 in comp.lang.python, Paul Rubin
<http://phr.cx@NOSPAM.invalid> wrote:


>There is a third choice which is the natural and obvious one: have the
>function do what its name indicates.  Return true if the arg is a
>digit and false otherwise.  If iterating over the whole string is
>useful (which it may be), then the function should have been named
>differently, like .isdigits instead of .isdigit.

Following your logic to its conclusion, had the name isdigits been
chosen, '1'.isdigits() should return False.  It's only one digit, not
more than one, as the plural would imply.

I, for one, don't see any utility in the dichotomy.  We only need
(should only have) one function.  I do agree that isdigits might have
been a better name, but we're stuck with isdigit for hysterical
raisins.  And it's logical that string functions work over a string
rather than its first character.

>
>FWIW, I've usually tested for digit strings with re.match.  It never
>occurred to me that isdigit tested a whole string.  

Someone's been trotting out that old jwz chestnut about regular
expressions and problems...  Not that I agree with it, but ISTM that
regular expressions are vast overkill for this problem.

Regards,
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list