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

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Dec 22 16:55:45 CET 2005


On Wed, 21 Dec 2005 13:58:01 -0800, Erik Max Francis wrote:

> Steven D'Aprano wrote:
> 
>> In that case, the name is misleadingly wrong. I suppose it is not likely
>> that it could be changed before Python 3?
> 
> Why?
> 
> The primary purpose of the .isdigit, etc. methods is to test whether a 
> single character has a certain property.  There is, however, no special 
> character data type in Python, and so by necessity those methods must be 
> on strings, not characters.
> 
> Thus, you have basically two choices:  Have the methods throw exceptions 
> for strings with a length different from one, or have them just iterate 
> over every character in a string.  The latter is clearly a more useful 
> functionality.

*shrug*

If your argument was as obviously correct as you think, shouldn't
ord("abc") also iterate over every character in the string, instead of
raising an exception?

But in any case, I was arguing that the *name* is misleading, not that the
functionality is not useful. (Some might argue that the functionality is
harmful, because it encourages Look Before You Leap testing.) In English,
a digit is a single numeric character. In English, "123 is a digit" is
necessarily false, in the same way that "A dozen eggs is a single egg" is
false.

In any case, it isn't important enough to break people's code. I'd rather
that the method isdigit() were called isnumeric() or something, but I can
live with the fact that it is not.


-- 
Steven.




More information about the Python-list mailing list