Ci3*_*Ci3 129 python python-3.x
我一直试图找出如何表示一个最大整数,我已经阅读使用"sys.maxint".但是,在Python 3中,当我调用它时,我得到:
AttributeError: module 'object' has no attribute 'maxint'
Run Code Online (Sandbox Code Playgroud)
Die*_*sch 141
删除了sys.maxint常量,因为不再对整数值进行限制.但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数.它符合实现的"自然"整数大小,并且通常与同一平台上以前版本中的sys.maxint相同(假设具有相同的构建选项).
http://docs.python.org/3.1/whatsnew/3.0.html#integers
mwf*_*ley 68
正如其他人所指出的,Python 3 int没有最大尺寸,但如果您只需要保证高于任何其他int值的东西,那么您可以使用Infinity的浮点值,您可以使用它float("inf").
gps*_*gps 22
Python 3 int没有最大值.
如果您的目的是在编译时以与Python相同的方式确定C中int的最大大小,则可以使用struct模块找出:
>>> import struct
>>> platform_c_maxint = 2 ** (struct.Struct('i').size * 8 - 1) - 1
Run Code Online (Sandbox Code Playgroud)
如果您对Python 3 int对象的内部实现细节感到好奇,请查看sys.int_info每位数和位数详细信息.没有正常的程序应该关心这些.
小智 9
如果您查找的数字大于其他所有数字:
方法1:
float('inf')
Run Code Online (Sandbox Code Playgroud)
方法2:
import sys
max = sys.maxsize
Run Code Online (Sandbox Code Playgroud)
如果您看到的数字小于所有其他数字
方法1:
float('-inf')
Run Code Online (Sandbox Code Playgroud)
方法2:
import sys
min = -sys.maxsize - 1
Run Code Online (Sandbox Code Playgroud)
方法1在Python2和Python3中均有效。方法2在Python3中有效。我没有在Python2中尝试方法2。
小智 5
Python 3.0 不再有 sys.maxint,因为 Python 3 的 int 是任意长度的。它没有 sys.maxint,而是 sys.maxsize;正尺寸 size_t 又名 Py_ssize_t 的最大尺寸。
| 归档时间: |
|
| 查看次数: |
166681 次 |
| 最近记录: |