2

I checked the help for sys.maxsize in Python 3.6:

>>> help(sys)
[...]
maxsize -- the largest supported length of containers.

Testing it:

In [10]: '{:,}'.format(sys.maxsize)
Out[10]: '9,223,372,036,854,775,807'

In [11]: math.log2(sys.maxsize)
Out[11]: 63.0

It's 63 bits, which suggests a leading sign bit. However, the length of a container cannot be negative.

What's going on here?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Alice
  • 1,360
  • 2
  • 13
  • 28
  • 1
    Possible duplicate of https://stackoverflow.com/questions/48138632/in-python-what-is-sys-maxsize – taurus05 Mar 01 '19 at 05:11
  • I don't think that's a duplicate; the suggested post simply says *that* `sys.maxsize` is the maximum container size, not *why* it's 63 bits, rather than 64. – Zero Piraeus Mar 01 '19 at 10:59

1 Answers1

1

The maximum size of a container was increased in Python 2.5 from 231-1 to 263-1. PEP 353: Using ssize_t as the index type, which introduced the change, says:

Why not size_t

An initial attempt to implement this feature tried to use size_t. It quickly turned out that this cannot work: Python uses negative indices in many places (to indicate counting from the end). Even in places where size_t would be usable, too many reformulations of code where necessary, e.g. in loops like:

for(index = length-1; index >= 0; index--)

This loop will never terminate if index is changed from int to size_t.

So, the limitation stems from the decision to use a Python-specific "indexing" type, which it was convenient to define as signed (ssize_t) rather than unsigned (size_t) in order to ease the handling of negative indices.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160