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

Michael H. Goldwasser goldwamh at slu.edu
Tue Mar 29 00:23:45 CEST 2011



On Monday March 28, 2011, Laura Creighton wrote: 

>    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.

Note well that the issue has changed since the original post.  The
question at the time concerned the following code

>>> a = 1
>>> b = a  # b and a are two different 1s

Carl correctly points out that b and a are not two different 1s in
this case. The assignment statment of a = b has the same semantics for
integers as it does for any other data type.  a and b become aliases
for the same underlying instance.  On any platform, it will be the
case that

>>> a is b
True

folowing that assignment.  The original misunderstanding involved the
semantics of b += 2; that command does not mutate the underlying
instance, but re-associates the identifier b to a new value (while a
remains associated with the value 1).

In your reply, the code fragment (below) shows a different issue
regarding the re-use of integer literals in source code (as you
assigned b=1 rather than b=a).  In that case, it is system-dependent
whether the interpretter uses different instances or the same
instance, and you are right that there is no guarantee that equivalent
instances will be identical instances.

With regard,
Michael

>    
>    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

+-----------------------------------------------
| Michael H. Goldwasser, Ph.D.
| Associate Professor
| Director of Computer Science
| Dept. Mathematics and Computer Science
| Saint Louis University
| 220 North Grand Blvd.
| St. Louis, MO 63103-2007
|
| Office: Ritter Hall 108
| Email:  goldwamh at slu.edu
| URL:    http://cs.slu.edu/~goldwasser
| Phone:  (314) 977-7039
| Fax:    (314) 977-1452



More information about the Edu-sig mailing list