How to test if object is an integer?

Ben Finney ben+python at benfinney.id.au
Fri Oct 14 21:51:41 EDT 2011


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 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.

-- 
 \      “When I was a little kid we had a sand box. It was a quicksand |
  `\           box. I was an only child... eventually.” —Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list