[Edu-sig] looking for explanations... globals dynamic dict

Laura Creighton lac at openend.se
Mon Mar 28 23:25:00 CEST 2011


In a message of Tue, 29 Mar 2011 09:59:40 +1300, Carl Cerecke writes:
>Well, not really. Although I see what you are trying to say.

>numbers (like strings) are immutable. There can ever be only one number 1
>You can't change a number to something else. If add 5 to 3, the number 3
>doesn't change, I get a new number, 8.

This is merely an implementation detail.

Python 2.6.6 
Type "help", "copyright", "credits" or "license" for more information.
>>> w = 1
>>> y = 1       
>>> w is y  # this might surprise you
True

[PyPy 1.4.1] 
Type "help", "copyright", "credits" or "license" for more information.

>>>> w = 1
>>>> y = 1
>>>> w is y # or are you so used to CPython that this surprises you?
False

You can have as many number ones as you like.  In CPython they are
cached to be the same object, but you shouldn't rely on that behaviour.
It's actually pretty dodgy to test the object identity on immutable
objects -- unless you want to surprise people like I tried to.

Incidentally

Python 2.6.6 
Type "help", "copyright", "credits" or "license" for more information.
>>> w = 5234
>>> y = 5324    
>>> w is y  
False

so after a certain point, the CPython developers have decided that the
performance gains of cacheing all number xs as the same x isn't worth
it.

Laura




More information about the Edu-sig mailing list