Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created May 1, 2012 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/2567864 to your computer and use it in GitHub Desktop.
Save ashwin/2567864 to your computer and use it in GitHub Desktop.
Range of integers cached by Python
# Python caches a small range of integers (objects)
# This is implementation specific and can be discovered easily
import platform
cacheBegin, cacheEnd = 0, 0
for i in range( -500, 0 ):
if i is int(str(i)):
cacheBegin = i
break
for i in range( cacheBegin, 500 ):
if i is not int(str(i)):
cacheEnd = i - 1
break
print( "Python version: {} implementation: {}".format( platform.python_version(), platform.python_implementation() ) )
print( "This implementation caches integers {} to {}".format( cacheBegin, cacheEnd ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment