Problem
How can I tell if a variable is an integer or not?
Asked by Hulk
Solution #1
If you have to do this,
isinstance(<var>, int)
Unless you’re using Python 2.x, in which case you should use
isinstance(<var>, (int, long))
Type is not to be used. It’s nearly never the proper answer in Python because it disables polymorphism’s flexibility. If you subclass int, for example, your new class should register as an int, which is not the case:
class Spam(int): pass
x = Spam(0)
type(x) == int # False
isinstance(x, int) # True
This corresponds to Python’s strong polymorphism: instead of requiring that an object act like an int, you should let any object that behaves like one.
The classical Python mentality, though, is that it’s easier to ask forgiveness than permission. In other words, don’t check whether x is an integer; assume that it is and catch the exception results if it isn’t:
try:
x += 1
except TypeError:
...
This mindset is gradually being replaced by the use of abstract base classes, which allow you to specify exactly what properties your object should have (adding? multiplying? doubling?) by inheriting from a specially-constructed class. That would be the greatest approach because it will allow just those objects having the required and adequate properties, but you’ll have to read the documentation to figure out how to utilize it.
Answered by Katriel
Solution #2
All of the above suggestions appear to overlook the fact that a double (floats in Python are doubles) can also be an integer (if it has nothing after the decimal point). To check this, I utilize the built-in is integer() method on doubles.
In a for loop, an example of what to perform every xth time is:
for index in range(y):
# do something
if (index/x.).is_integer():
# do something special
Edit:
Before calling this method, you can always convert to a float. The three options are:
>>> float(5).is_integer()
True
>>> float(5.1).is_integer()
False
>>> float(5.0).is_integer()
True
Otherwise, as Agostino suggested, you might verify if it’s an int first:
def is_int(val):
if type(val) == int:
return True
else:
if val.is_integer():
return True
else:
return False
Answered by saroele
Solution #3
Here’s a rundown of the various ways mentioned:
and here’s how they apply to a variety of integer-valued numerical types:
They aren’t quite constant, as you can see. Although Fraction and Rational are essentially similar, one has a.index() method while the other does not. Even if the real part is integral and the imaginary part is 0, complex types dislike converting to int.
(np.int8|16|32|64(5) denotes that np.int8(5), np.int32(5), and so on behave exactly the same.)
Answered by endolith
Solution #4
If you truly need to inspect anything, abstract base classes are preferable to concrete ones. For an integer, this means:
>>> import numbers
>>> isinstance(3, numbers.Integral)
True
This means that the check isn’t limited to simply int or int and long, but also additional user-defined types that behave like integers.
Answered by Scott Griffiths
Solution #5
>>> isinstance(3, int)
True
For more information, go here.
If you’re seeking for int-like qualities, this won’t help you. In this situation, you need additionally look for long:
>>> isinstance(3L, (long, int))
True
In the Python source, I’ve seen checks like this against an array/index type, but I don’t believe that’s observable outside of C.
Response from a token SO: Are you certain you need to check its type? Either don’t pass a type you can’t handle, or don’t try to outsmart your potential code reusers, they may have a good reason not to pass an int to your function.
Answered by Matt Joiner
Post is based on https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not