[Tutor] Can someone explain this to me please

eryksun eryksun at gmail.com
Sat Aug 22 14:00:11 CEST 2015

On Fri, Aug 21, 2015 at 1:04 PM, Jon Paris <jon.f.paris at gmail.com> wrote:
> import sys
> x = sys.maxsize
> print ("Max size is: ", x)
> y = (x + 1)
> print ("y is", type(y), "with a value of", y)
> Produces this result:
> Max size is:  9223372036854775807
> y is <class 'int'> with a value of 9223372036854775808
> I was expecting it to error out but instead it produces a value greeter than the
> supposed maximum while still keeping it as an int. I’m confused. If
> sys.maxsize _isn’t_ the largest possible value then how do I determine what is?

sys.maxsize is the "maximum size lists, strings, dicts, and many other
containers can have". This value is related to the theoretical maximum
of Python's built-in arbitrary precision integer type (i.e. long in
Python 2 and int in Python 3), which can be thought of as a
'container' for 15-bit or 30-bit "digits". For example, in a 64-bit
version of Python 3 that's compiled to use 30-bit digits in its int
objects, the limit is about (sys.maxsize bytes) // (4 bytes /
30bit_digit) * (9 decimal_digits / 30bit_digit) ==
20752587082923245559 decimal digits. In practice you'll get a
MemoryError (or probably a human impatience KeyboardInterrupt) long
before that.

sys.maxint (only in Python 2) is the largest positive value for Python
2's fixed-precision int type. In pure Python code, integer operations
seamlessly transition to using arbitrary-precision integers, so you
have no reason to worry, practically speaking, about reaching the
"largest possible value". As a matter of trivia, sys.maxint in CPython
corresponds to the maximum value of a C long int. In a 64-bit Windows
process, a C long int is 32-bit, which means sys.maxint is 2**31 - 1.
In every other supported OS, sys.maxint is 2**63 - 1 in a 64-bit

More information about the Tutor mailing list