Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "constant sharing" works differently in REPL than in script ?

Reply
Thread Tools

"constant sharing" works differently in REPL than in script ?

 
 
shearichard@gmail.com
Guest
Posts: n/a
 
      06-19-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 .




 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      06-19-2012
On Mon, Jun 18, 2012 at 7:52 PM, <(E-Mail Removed)> wrote:
> 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 fora 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 .
>


Python the language doesn't specify anything about this sort of
behavior. CPython the implementation does all sorts of optimizations
to make code run more efficiently. Caching integers is one of those
optimizations. In the case where the code is compiled all at once (as
in the script) instead of one line at a time (the REPL), it can do
more optimizations. But as I said, none of this is in the
specification so you shouldn't rely on it. The general rule for the
"is" operator is that unless you specifically know that you need it,
don't use it.
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      06-19-2012
On Tue, Jun 19, 2012 at 12:52 PM, <(E-Mail Removed)> 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
 
Reply With Quote
 
shearichard@gmail.com
Guest
Posts: n/a
 
      06-19-2012
Thanks for all the replies. I hadn't thought about the opportunities that exist for optimization when the whole script is there (or when compound operations are taking place) by contrast with plain old REPL ops.

I liked your code Chris demoing the different ranges in different versions. I tried to write something like that myself but you did it an awful lot better !

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-19-2012
On Wed, Jun 20, 2012 at 7:21 AM, <(E-Mail Removed)> wrote:
> I liked your code Chris demoing the different ranges in different versions. I tried to write something like that myself but you did it an awful lot better !


There's no guarantee that it'll prove which are and aren't cached, but
it does seem to work. (Incidentally, it doesn't handle the case where
_no_ integers are cached.)

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
url mapping works differently in Cassini than in IIS?? Les Caudle ASP .Net 2 09-19-2006 04:53 PM
SnapShot Repl. Sivaraman Latchapathi MCSE 0 07-11-2005 09:36 AM
JS file handled differently than in-line script block bas jaburg ASP .Net 0 02-11-2005 08:25 AM
urgent help needed, Server.MapPath() works differently on server than on development machine Peter Row ASP .Net 0 03-01-2004 08:49 AM
<SELECT OnChange=foo()> works differently in NS 7 than in other browsers Steven Kan Javascript 0 08-19-2003 07:52 PM



Advertisments