Damgaard
Damgaard

Reputation: 942

Version agnotstic checking if a variable is an integer?

In python 2.6 and 2.7 you use

isinstance(variable, (int, long))

In python 3x ints and longs are merged. So you simply do

isinstance(variable, int)

Is there a clean version agnostic way of testing if a variable is an integer in python?

Upvotes: 1

Views: 77

Answers (1)

Jon Clements
Jon Clements

Reputation: 142206

From 2.6 you can use numbers.Integral:

if isinstance(var, numbers.Integral):
    pass # whatever

Upvotes: 2

Related Questions