【发布时间】:2014-03-19 17:19:18
【问题描述】:
自纪元以来的当前毫秒数为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 参数的方法中)
【问题讨论】:
-
绕过什么,究竟是什么?您要避免以不同值发生 int/long 转换的什么后果?
-
请注意,即使您有 64 位 Windows,您仍然可以安装 32 位 Python,这里似乎就是这种情况。
-
请注意,
int(20000000000000000000)将在 python 32 位和 64 位中返回long,因为该数字大于2**64。从int到long的转换几乎总是静默执行,因为除了操作性能之外,行为不应该有任何显着变化。我不知道在整数类型之间引发OverflowError的情况。
标签: python