Is The Small Number Cache Defined In Python Specification Or Is It An Implementation Detail?
Python seems to have a so called 'small number cache' for numbers in the range -5 to 256. We can demonstrate that with the following program: for i in range(-7, 258): if id(i)
Solution 1:
It's an implementation detail: chapter 3.1 of the Python language reference (all versions, from 2.7 through 3.9) says:
for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value
and
E.g., after
a = 1; b = 1
,a
andb
may or may not refer to the same object with the value one, depending on the implementation
(emphasis mine)
The Python/C API reference also mentions it
The current implementation keeps an array of integer objects for all integers between -5 and 256,
(emphasis mine) which is also quite clear that this might change in the future.
Post a Comment for "Is The Small Number Cache Defined In Python Specification Or Is It An Implementation Detail?"