"constant sharing" works differently in REPL than in script ?
Chris Angelico
rosuav at gmail.com
Tue Jun 19 04:37:22 EDT 2012
On Tue, Jun 19, 2012 at 12:52 PM, <shearichard at gmail.com> wrote:
> ...Python pre creates some integer constants to avoid a proliferation of objects with the same value.
>
> I was interested in this and so I decided to try it out.
> So that matched what I'd heard and then I did this to test the limits of it :
>
> And that was reasonable too as the podcast mentioned it was only done for a small set of integers around zero.
The exact set varies according to Python version and, as others have
mentioned, shouldn't be relied upon.
import sys
print(sys.version)
wascached=False
for i in range(-100,300):
j=i+1
j-=1
if (i is j)!=wascached:
wascached=i is j
if wascached:
firstcache=i
else:
print("%d to %d are cached"%(firstcache,i-1))
break
2.4.5 (#1, Jul 22 2011, 02:01:04)
[GCC 4.1.1]
-5 to 99 are cached
2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
-5 to 256 are cached
3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)]
-5 to 256 are cached
It's definitely something that's fun to play with, though not
something to take ANY notice of in real code :)
ChrisA
More information about the Python-list
mailing list