Python 3中的sys.maxint是什么?

时间:2022-02-27 10:46:51

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'

5 个解决方案

#1


99  

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相同(假设具有相同的构建选项)。

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

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

#2


42  

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

正如其他人所指出的,Python 3的int没有最大大小,但是如果你只需要保证高于任何其他int值的东西,那么你可以使用浮点值为Infinity,你可以用浮点数获得( “INF”)。

#3


17  

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

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

#4


13  

Python 3 doesn't have limit on int. but you can use sys.maxsize as an integer larger than any practical list or string index.

Python 3对int没有限制。但您可以将sys.maxsize用作大于任何实际列表或字符串索引的整数。

#5


1  

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的int是任意长度的。而不是sys.maxint,它有sys.maxsize;正大小size_t的最大大小,也就是Py_ssize_t。

#1


99  

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相同(假设具有相同的构建选项)。

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

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

#2


42  

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

正如其他人所指出的,Python 3的int没有最大大小,但是如果你只需要保证高于任何其他int值的东西,那么你可以使用浮点值为Infinity,你可以用浮点数获得( “INF”)。

#3


17  

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

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

#4


13  

Python 3 doesn't have limit on int. but you can use sys.maxsize as an integer larger than any practical list or string index.

Python 3对int没有限制。但您可以将sys.maxsize用作大于任何实际列表或字符串索引的整数。

#5


1  

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的int是任意长度的。而不是sys.maxint,它有sys.maxsize;正大小size_t的最大大小,也就是Py_ssize_t。