General Type Checks (int, str, tuple, etc.)

Scott David Daniels scott.daniels at acm.org
Sat Jan 28 18:33:09 CET 2006


Fabian Steiner wrote:
> Hello!
> 
> So far, I am using something like »if isinstance(var, int):« to 
> determine, whether var's value is an integer. Now I would like to know 
> if there is any better possibility to do such general checks or may a 
> construct with isinstance() even fail in certain cases?

The general rule is: don't check, let it fail if it wants to.
If you are looking for an integral value check, you might prefer:

    isinstance(x, (int, long))

but again, think long and hard about your requirements.  Embrace
dynamic typing; don't try to write your old language in Python.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list