>>> a = 1
>>> b = 2
>>> id(a), id(b), id(b) - id(a)
(1814458401008, 1814458401040, 32)
Is the memory address returned by id
in bits or in bytes? Per the docs:
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.
If integers were 32 bits and the numbers -5 to 256 were stored close together in memory, then the numbers 1 and 2 would be 32 bits apart.
However, the size of a number object is 28 bytes.
>>> import sys
>>> a = 1
>>> a.__sizeof__(), sys.getsizeof(a), sys.getsizeof(3)
(28, 28, 28)
If id returns an address in bytes, what is the 32-28=4 bytes between 1 and 2 for?
For bigger numbers, the addresses are random which makes sense:
>>> a = 257
>>> b = 258
>>> id(a), id(b), id(b) - id(a)
(1814557834096, 1814557827568, -6528)
jasonharper answered in a comment, it's to make the address 8 bytes aligned. For more details, go to this answer.