"constant sharing" works differently in REPL than in script ?
shearichard at gmail.com
shearichard at gmail.com
Mon Jun 18 22:52:53 EDT 2012
Listening to 'Radio Free Python' episode 8 (http://radiofreepython.com/episodes/8/ - around about the 30 minute mark) I heard that 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.
First I did this at the prompt :
>>> c = 1
>>> print id(1)
26906152
>>> print id(c)
26906152
>>> c is 1
True
So that matched what I'd heard and then I did this to test the limits of it :
>>> c = 259
>>> print id(259)
26167488
>>> print id(c)
26167512
>>> c is 259
False
And that was reasonable too as the podcast mentioned it was only done for a small set of integers around zero.
However when I wrote this script :
c = 259
print id(259)
print id(c)
if c is 259:
print "%s - yes" % (c)
else:
print "%s - no " % (c)
I got this output :
C:\data\src\Python\foo>python untitled-2.py
26760884
26760884
259 - yes
So what's going on here. The script seems to be sharing objects in a way the REPL isn't ?
Can anyone explain please ?
BTW this is all on : Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 .
More information about the Python-list
mailing list