The current number of milliseconds since the epoch is 1395245378429; on unix (64 bit / Ubuntu / python 2.7), you can do:
>>> t = 1395245378429
>>> type(t)
<type 'int'>
>>> t = 1395245378429L
>>> type(t)
<type 'long'>
>>> int(t)
1395245378429
>>> type(int(t)
<type 'int'>
but on Windows (also 64 bit / python 2.7), this happens:
>>> t = 1395245378429
>>> type(t)
<type 'long'>
>>> int(t)
1395245378429L
>>> type(int(t))
<type 'long'>
so, the following weird observations:
int(<long>) returns a longI can't see anything obvious in the documentation to say this is correct behaviour; is there a (correct) way to convert the long to an int (i.e. so it can be used in a method which requires an int argument)
Python uses C long for the int type, and even on Windows this is limited to 32-bit. You can see your platform's current maximum native int size by inspecting the sys.maxint value:
The largest positive integer supported by Python’s regular integer type. This is at least 2**31-1. The largest negative integer is
-maxint-1— the asymmetry results from the use of 2’s complement binary arithmetic.
and from the Numeric Types section:
Plain integers (also just called integers) are implemented using
longin C, which gives them at least 32 bits of precision (sys.maxintis always set to the maximum plain integer value for the current platform, the minimum value is-sys.maxint - 1).
Unless you are directly interacting with a C extension library that doesn't support the Python long type, there is no need to worry about when Python uses int and when you need to use long. In Python 3, the separate long type has been removed entirely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With