What is sys.maxint in Python 3?

PythonPython 3.x

Python Problem Overview


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'

Python Solutions


Solution 1 - Python

> 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

Solution 2 - Python

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

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

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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.

Solution 6 - Python

An alternative is

import math

... math.inf ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCi3View Question on Stackoverflow
Solution 1 - PythonDiego BaschView Answer on Stackoverflow
Solution 2 - PythonmwfearnleyView Answer on Stackoverflow
Solution 3 - PythonVikrantView Answer on Stackoverflow
Solution 4 - PythongpsView Answer on Stackoverflow
Solution 5 - PythonCookiesflyView Answer on Stackoverflow
Solution 6 - PythonwstomvView Answer on Stackoverflow