问题:Python 3中的sys.maxint是什么?

我一直在尝试找出如何表示最大整数,并且已经阅读过使用"sys.maxint"。但是,在Python 3中,当我调用它时,会得到:

AttributeError: module 'object' has no attribute 'maxint'

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:

AttributeError: module 'object' has no attribute 'maxint'

回答 0

sys.maxint常量已删除,因为整数值不再受限制。但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。它符合实现的“自然”整数大小,并且通常与同一平台上的先前版本中的sys.maxint相同(假定具有相同的生成选项)。

http://docs.python.org/3.1/whatsnew/3.0.html#integers

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).

http://docs.python.org/3.1/whatsnew/3.0.html#integers


回答 1

正如其他人指出的那样,Python 3 int没有最大大小,但是如果您只需要保证比其他任何int值都高的值,则可以将Infinity的float值使用float("inf")

As pointed out by others, Python 3’s int does not have a maximum size, but if you just need something that’s guaranteed to be higher than any other int value, then you can use the float value for Infinity, which you can get with float("inf").


回答 2

Python 3 int没有最大值。

如果您的目的是在以与Python相同的方式确定C语言中int的最大大小,则可以使用struct模块找出:

>>> import struct
>>> platform_c_maxint = 2 ** (struct.Struct('i').size * 8 - 1) - 1

如果您对Python 3 int对象的内部实现细节感到好奇,请查看sys.int_info每位位数和位数大小的详细信息。没有正常的程序应该关心这些。

Python 3 ints do not have a maximum.

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:

>>> 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_info for bits per digit and digit size details. No normal program should care about these.


回答 3

如果您要查找的数字大于所有其他数字:

方法1:

float('inf')

方法2:

import sys
max = sys.maxsize

如果您要寻找一个小于所有其他数字的数字:

方法1:

float('-inf')

方法2:

import sys
min = -sys.maxsize - 1

方法1在Python2和Python3中均有效。方法2在Python3中有效。我没有在Python2中尝试方法2。

If you are looking for a number that is bigger than all others:

Method 1:

float('inf')

Method 2:

import sys
max = sys.maxsize

If you are looking for a number that is smaller than all others:

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.


回答 4

由于Python 3的int具有任意长度,因此Python 3.0不再具有sys.maxint。它具有sys.maxsize而不是sys.maxint;正数size_t或Py_ssize_t的最大大小。

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.


回答 5

另一种方法是

import math

... math.inf ...

An alternative is

import math

... math.inf ...

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。