224

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'
1

6 Answers 6

238

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

https://docs.python.org/3/whatsnew/3.0.html#integers

2
  • 11
    That's all nice and good, until you need a timedelta(days=x) and x is larger than 2^32: then you get OverflowError: Python int too large to convert to C int.
    – marcotama
    Feb 24, 2021 at 11:48
  • @marcotama datetime.timedelta can only go up to days=999_999_999 anyway. You should probably use a different library.
    – wjandrea
    Feb 4 at 18:59
104

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.

2
  • Also works with legacy Python (2.7) - this is the correct answer when you need to return a number higher than all possible real values (e.g. for list sorting by comparing objects)
    – Aaron D
    Aug 2, 2017 at 11:56
  • 15
    One downside of this approach is that float inf can ruin the data type of arrays if you are trying to apply jit compilation with numba or Cython or similar. You may need a Python list of all integer types so that a jit compiler can automatically convert it to the appropriate c type, for example. This is especially critical for classical algorithms where you might apply jit tools to make them much faster than a pure Python implementation, but still want to write the implementation in pure Python for convenience, testing, fallback if jit compiler is not installed in a deployed environment, etc.
    – ely
    Mar 3, 2019 at 23:18
44

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
  • Hi sys.maxsize only returns 9223372036854775807 this, if your number goes greater than that it return False (ie) your_large_no<sys.maxsize will return False Jan 12, 2021 at 20:44
  • 3
    …Please use math.inf and -math.inf instead of parsing a string.
    – Will Chen
    Jun 19, 2021 at 18:05
  • 1
    It is ok to use either math.inf or 'inf'
    – Vikrant
    Jun 21, 2021 at 16:35
  • It is a little shorter, a little clearer and a tiny bit faster - one less function call - to use math.inf over float('inf')
    – Tom Swirly
    Jul 15, 2022 at 9:15
31

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.

1
  • 3
    Note that a python 2 int was the equivilent of a C long, not a C int.
    – plugwash
    Oct 23, 2020 at 4:29
5

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.

1

An alternative is

import math

... math.inf ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.