自纪元以来的当前毫秒数是1395245378429;在unix(64位/ Ubuntu / python 2.7)上,你可以这样做: t = 1395245378429 type(t)type int t = 1395245378429L type(t)type long int(t)13952...

自纪元以来的当前毫秒数是1395245378429;在unix(64位/ Ubuntu / python 2.7)上,你可以这样做:
>>> t = 1395245378429
>>> type(t)
<type 'int'>
>>> t = 1395245378429L
>>> type(t)
<type 'long'>
>>> int(t)
1395245378429
>>> type(int(t)
<type 'int'>
但在Windows(也是64位/ python 2.7)上,会发生这种情况:
>>> t = 1395245378429
>>> type(t)
<type 'long'>
>>> int(t)
1395245378429L
>>> type(int(t))
<type 'long'>
那么,以下奇怪的观察:
>在Windows上,int(< long>)返回一个long
>在Windows中将相同的数字视为long,但在unix上视为int
我在文档中看不到任何明显的东西,说这是正确的行为;有没有(正确的)方法将long转换为int(即所以它可以在需要int参数的方法中使用)
解决方法:
Python使用C long作为int类型,甚至在Windows上也限制为32位.您可以通过检查sys.maxint value来查看平台当前最大的原始int大小:
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.
从Numeric Types section开始:
Plain integers (also just called integers) are implemented using
long
in C, which gives them at least 32 bits of precision (sys.maxint
is always set to the maximum plain integer value for the current platform, the minimum value is-sys.maxint - 1
).
除非您直接与不支持Python long类型的C扩展库进行交互,否则无需担心Python何时使用int以及何时需要使用long.在Python 3中,完全删除了单独的long类型.
本文标题为:python在Windows和Unix上处理不同的长整数


基础教程推荐
- 在单个python进程中混合绿色线程和本机线程是否安全? 2023-11-14
- Python – 在守护进程中调用multiprocessing.pool 2023-11-16
- Python之进程 2023-09-04
- Python中的getattr、__getattr__、__getattribute__、__get__详解 2023-08-04
- windows下python虚拟环境virtualenv安装和使用 2023-09-05
- Python调用百度AI实现人像分割详解 2023-08-04
- 基于Python实现视频转字符画动漫小工具 2023-08-04
- linux 安装 python 最全教程 2023-11-15
- 从python调用进程的最快方法? 2023-11-15
- 【网站】python的Windows平台的扩展包 2023-09-03