How to test if object is an integer?

Terry Reedy tjreedy at udel.edu
Sat Oct 15 02:03:50 EDT 2011


On 10/14/2011 9:51 PM, Ben Finney wrote:
> Terry Reedy<tjreedy at udel.edu>  writes:
>
>> On 10/14/2011 9:05 PM, Chris Angelico wrote:
>
>>> That tests if the object is already an int; the OP asked if a string
>>> contains an integer.
>>
>> The misleading subject line did not. It should have been "How to test
>> if a string contains an integer?"

> Which would still be misleading :-)
>
> Even better is “How to test whether a string is a valid representation
> of an integer?”

I agree, but that is more than I would ask of a newbie, whereas asking 
people to ask the same question in subject line and text, even if the 
question is inadequate, is reasonable.

> I say that's better because it gets to the relevant point of asking
> *which* representations you want to test for – what qualifies as valid
> for your particular use case, and what does not. There's no single right
> answer; the programmer must choose exactly what they want to test for.

Yes. Even the wrong subject line question is ambiguous, as any of int, 
bool, float, complex, decimal.Decimal, and fractions.Fraction can have 
an integer value, as might user class instances, and, of course, 
depending on context, bytes and strings.

-- 
Terry Jan Reedy





More information about the Python-list mailing list