[Tutor] checking if a variable is an integer?

Hugo Arts hugo.yoshi at gmail.com
Wed Jun 1 00:02:22 CEST 2011


---------- Forwarded message ----------
From: Hugo Arts <hugo.yoshi at gmail.com>
Date: Wed, Jun 1, 2011 at 12:01 AM
Subject: Re: [Tutor] checking if a variable is an integer?
To: Peter Lavelle <lists at solderintheveins.co.uk>


On Tue, May 31, 2011 at 11:46 PM, Peter Lavelle
<lists at solderintheveins.co.uk> wrote:
> I think you could also use the type() function. See example below:
>
> if type(yourvar) == int:
>     #Do stuff here if it is an integer
>
> else:
>      #Do something here if it is not an integer
>

I think the isinstance() function is preferred since it also catches subclasses:

http://docs.python.org/library/functions.html#isinstance

Hugo


More information about the Tutor mailing list