This is interpreter dependent (i.e. there are no specifications that require such caching). But as far as I know the python
interpreter has a cache for integers up to and including 256. Furthermore values up to and including -5 are cached as well. So the range is -5 to 256 (both included), like written in the documentation:
The current implementation keeps an array of integer objects for all integers between -5 and 256 (..)
You thus better never use reference equality to check whether two integers are equal, always use ==
. This is also useful if you for instance would compare an int
against a numpy int16
. If you use reference checks, the following will fail:
>>> np.int16(12) is 12
False
>>> np.int16(12) == 12
True