python在Windows和Unix上处理不同的长整数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python在Windows和Unix上处理不同的长整数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1384字,纯文字阅读大概需要2分钟。
内容图文
![python在Windows和Unix上处理不同的长整数](/upload/InfoBanner/zyjiaocheng/950/11517bc59f8b40c79f0804da9c12ca7f.jpg)
自纪元以来的当前毫秒数是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.
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在Windows和Unix上处理不同的长整数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。