[Tutor] Can someone explain this to me please

Alan Gauld alan.gauld at btinternet.com
Fri Aug 21 21:37:23 CEST 2015


On 21/08/15 19:04, Jon Paris wrote:

> Max size is:  9223372036854775807
> y is <class 'int'> with a value of 9223372036854775808
>
> I was expecting it to error out

The documentation says:
-------------------
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.
-------------------

So from your output we can deduce that Python ints (in version 3)
are not stored in a Py_ssize_t variable. (In Python 2 they are
so what you expect is almost what you would get - except
Python converts int to long int automatically.)

Python 3 ints are long ints, which the documentation says means
they have "infinite precision" In practice their max size depends
on how much memory you have!

There are others here who can give you the inner details of how
it is all done, but for most programmers that's all you normally
need to know.

Where you can run into issues is where you use modules that,
in turn, use the underlying C libraries to do calculations.
Passing a long int to them can result in errors.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list