Why does CPython not cache integer values outside the range [-5, 256]?
I was experimenting with variable initialization in Python and I came across a scenario where the id(integers) in a certain range would be the same and the id(integers) greater than 256, Python creates a new reference. Ex: >>>> a = 2 >>>> b = 2 >>>> id(a) == id(b) True >>>> a = 257 >>>> b = 257 >>>> id(a) == id(b) False Reading through the documentation, it is clear that Python caches small integer values during initialization. https://hg.python.org/cpython/file/tip/Objects/longobject.c#l5343 Why does Python internally cache only integer values in the range [-5, 257) During initialization? Als
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.