How to test if object is an integer?

Chris Rebert clp2 at rebertia.com
Fri Oct 14 21:10:56 EDT 2011


On Fri, Oct 14, 2011 at 6:05 PM, Chris Angelico <rosuav at gmail.com> wrote:
> 2011/10/15 惜悯 <wauue at qq.com>:
>> retrun True if type(i) is int else False
>
> That tests if the object is already an int; the OP asked if a string
> contains an integer.

Additionally:
* the if-then-else there is unnecessary since `type(i) is int` already
returns a bool
* such a type test is normally and better written `isinstance(i, int)`

Cheers,
Chris R.



More information about the Python-list mailing list