I want to have a input type check for an API function that works in Python 2.7 and up. The API takes a timestamp in milliseconds since epoch as its parameter. I need to ensure that the input is a positive number.
Depending on the value, Python 2.7 will represent a timestamp as an integer
or a long
. So the type check would look like this:
isinstance(timestamp, (int, long))
However, the long
type was merged with int
for Python 3. In fact, the long type no longer exists. So the above line would cause an exception. Instead, the check would look like this:
isinstance(timestamp, int)
For compatibility with Python 2.7, I tried casting the timestamp to an int. However, the cast operation still returns a long
if the value is outside of the integer
range. This means the check will fail for any timestamp after Sun Jan 25 1970 20:31:23
. See also the answer to this question.
What would be the best way to make this a generic check that works in both versions of Python?