Python 3 中的 sys.maxint 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13795758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is sys.maxint in Python 3?
提问by Ci3
I've been trying to find out how to represent a maximum integer, and I've read to use "sys.maxint". However, in Python 3 when I call it I get:
我一直在试图找出如何表示最大整数,并且我已经阅读使用"sys.maxint". 但是,在 Python 3 中,当我调用它时,我得到:
AttributeError: module 'object' has no attribute 'maxint'
采纳答案by Diego Basch
The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation's “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).
删除了 sys.maxint 常量,因为对整数的值不再有限制。但是, sys.maxsize 可以用作比任何实际列表或字符串索引都大的整数。它符合实现的“自然”整数大小,并且通常与同一平台上以前版本中的 sys.maxint 相同(假设相同的构建选项)。
回答by gps
Python 3 ints do not have a maximum.
Python 3 int 没有最大值。
If your purpose is to determine the maximum size of an int in C when compiled the same way Python was, you can use the struct module to find out:
如果您的目的是在以与 Python 相同的方式编译时确定 C 中 int 的最大大小,您可以使用 struct 模块来找出:
>>> import struct
>>> platform_c_maxint = 2 ** (struct.Struct('i').size * 8 - 1) - 1
If you are curious about the internal implementation details of Python 3 int objects, Look at sys.int_infofor bits per digit and digit size details. No normal program should care about these.
如果您对 Python 3 int 对象的内部实现细节感到好奇,请查看sys.int_info每位数位和位数大小的详细信息。没有正常的程序应该关心这些。
回答by mwfearnley
As pointed out by others, Python 3's intdoes not have a maximum size, but if you just need something that's guaranteed to be higher than any other intvalue, then you can use the float value for Infinity, which you can get with float("inf").
正如其他人指出的那样,Python 3int没有最大大小,但是如果您只需要保证比任何其他int值都高的值,那么您可以使用浮点值来表示 Infinity,您可以使用float("inf").
回答by Cookiesfly
Python 3.0 doesn't have sys.maxint any more since Python 3's ints are of arbitrary length. Instead of sys.maxint it has sys.maxsize; the maximum size of a positive sized size_t aka Py_ssize_t.
Python 3.0 不再有 sys.maxint ,因为 Python 3 的整数是任意长度的。而不是 sys.maxint 它有 sys.maxsize; 正大小的 size_t 又名 Py_ssize_t 的最大大小。
回答by Vikrant
If you are looking number bigger than all others:
如果您正在寻找比所有其他人都大的数字:
Method 1:
方法一:
float('inf')
Method 2:
方法二:
import sys
max = sys.maxsize
If you are looking number smaller than all othes
如果您正在寻找比其他所有人都小的数字
Method 1:
方法一:
float('-inf')
Method 2:
方法二:
import sys
min = -sys.maxsize - 1
Method 1 works in both Python2 and Python3. Method 2 works in Python3. I have not tried Method 2 in Python2.
方法 1 适用于 Python2 和 Python3。方法 2 适用于 Python3。我还没有在 Python2 中尝试过方法 2。
回答by Tom Verhoeff
An alternative is
另一种选择是
import math
... math.inf ...

