Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is sys.maxint in Python 3?

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'
like image 886
Ci3 Avatar asked Oct 07 '22 21:10

Ci3


People also ask

What is the value of Maxint in Python?

value, which corresponds to 18,446,744,073,709,551,615 for the unsigned data type and ranges from -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 in the signed version. if we're running it on a 32-bit machine or higher.

How do I get Maxint in Python?

maxint does not exist as there is no limit or maximum value for an integer data type. But we can use the sys. maxsize to get the maximum value of the Py_ssize_t type in Python 2 and 3. It is also the maximum size lists, strings, dictionaries, and similar container types can have.

What is the biggest number Python can handle?

It's usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.


2 Answers

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

like image 172
Diego Basch Avatar answered Oct 18 '22 03:10

Diego Basch


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

In Python 3.5 onwards, you can use math.inf.

Note: as per ely's comment, this may impact the efficiency of your code, so it may not be the best solution.

like image 22
mwfearnley Avatar answered Oct 18 '22 02:10

mwfearnley