47

I assumed that this number ( 2^63 - 1 ) was the maximum value python could handle, or store as a variable. But these commands seem to be working fine:

>>> sys.maxsize 9223372036854775807 >>> a=sys.maxsize + 1 >>> a 9223372036854775808 

So is there any significance at all? Can Python handle arbitrarily large numbers, if computation resoruces permitt?

Note, here's the print-out of my version is:

>>> sys.version 3.5.2 |Anaconda custom (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]' 
3
  • 1
    "The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have." docs.python.org/2/library/sys.html The 3 site description is more vague. I'm not sure if it's the same in 2 and 3. Commented Jan 7, 2018 at 15:29
  • 2
    The maximum number of elements a list can contain. It is not the largest value an int can have, but the largest index the basic structures can contain, since outside this scope, it is no longer addressable. Commented Jan 7, 2018 at 15:31
  • Integers can be arbitrarily large in python, yes. Python is great for project euler. Obviously it's not infinite -- if the integer itself won't fit in memory, you can't do that. Commented Jan 7, 2018 at 15:37

2 Answers 2

65

Python can handle arbitrarily large integers in computation. Any integer too big to fit in 64 bits (or whatever the underlying hardware limit is) is handled in software. For that reason, Python 3 doesn't have a sys.maxint constant.

The value sys.maxsize, on the other hand, reports the platform's pointer size, and that limits the size of Python's data structures such as strings and lists.

Sign up to request clarification or add additional context in comments.

1 Comment

It's the width of the address register: the number of different memory locations that the processor can physically refer to.
10

Documentation for sys.maxsize:

An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform. python3

The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have. python2

What is Py_ssize_t?

It is an index type (number type for indexing things, like lists). It is the signed version of size_t (from the C language).

  • We don't use a normal number/ int, because this is unbounded in Python.
  • In Python, we don't use size_t because we want to support negative indexing, in Python we can do my_list[-4:]. So Py_ssize_t provides negative and positive numbers between a range.
  • The _t stands for type, to inform developers that size_t is a type name, not a variable. Just a convention.

So what is the effect of having a limit on Py_ssize_t? Why does this limit list, strings, dict size?

  • There is no way to index a list with an element larger than this. The list cannot get bigger than this, because it won't accept a non Py_ssize_t.
  • In the dictionary case, Py_ssize_t is used as the hash. Python doesn't use linked lists in its dictionary implementation, it uses Open Addressing/ probing, where if a collision is found, we a systematic way of getting another place to find the key and put the value. So you can't have more than Py_ssize_t in a dictionary in Python.

In all practical cases (64 bit machines aka. probably you), you will run out of memory before you max out Py_ssize_t. Trying dict.fromkeys(range(sys.maxsize + 5)) never got there, it just slowed my computer down.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.