> > How can I check if a variable is an integer?
>
> Luke and John have answered your question, but we should also ask, why
> do you want to do that? Explicit type testing is a code smell, perhaps
> there is a better way to do what you want.


- another way of doing it is: type(aVar) is int (which is faster than
using "==")
- if you want to check for ints and all subclasses of int: isinstance(aVar, int)
- if you want to check for ints, longs, and all subclasses:
isinstance(aVar, (int, long))

because python is strongly yet duckly-typed, there are circumstances
where you want to do some self introspection to find out what kind of
objects you've got.  this obviates the need for Python to support
multiple signatures, function overloading, etc. with that said,
however, it would also be a good idea to see what your application is
and determine if this kind of checking is warranted.

HTH!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to