Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of bytes required to store an integer in Python [duplicate]

I checked maximum size of the integer Python holds using sys.maxsize and it returned me 9223372036854775807.

Then why I can still store a value greater than this range?

How many bytes are required to store an integer and do Python changes number of bytes depending on the size of an integer?

I am using Python 3.6

like image 354
thatisvivek Avatar asked Oct 17 '25 08:10

thatisvivek


1 Answers

If you check the docs for sys.maxsize, you'll see

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.

There's nothing in there about Python ints! It's talking about Py_ssize_t, an internal C API thing with no practical relevance to working with Python ints.

Python ints use an arbitrary-precision representation that will use more bytes of memory to represent bigger integers. They are not limited by the values of Py_ssize_ts.

like image 181
user2357112 supports Monica Avatar answered Oct 18 '25 23:10

user2357112 supports Monica