Seemingly odd 'is' comparison.

Terry Reedy tjreedy at udel.edu
Tue Feb 19 00:51:05 EST 2008


"Asun Friere" <afriere at yahoo.co.uk> wrote in message 
news:3145ed12-d0e3-4d3a-9cb7-91efa6f17edb at d4g2000prg.googlegroups.com...
| So was that a yes or no?  I mean is it even possible for the identity
| behaviour of mutables to vary between implementations?  I can't see
| how they can possibly be interned, but is there some other factor I'm
| missing in regard to identity behaviour which could in fact vary
| between implementations?

Not that I can think of,  so 'No'.  The semantics of creation, rebinding, 
and copying are well defined and the behavior is predictable once one 
understands the rules.  The problem some new Pythoneers have is mistakenly 
thinking that binding statements ('=') make copies, or that assignments in 
the body of a class statement or function header (default values) are made 
more than once (per class or function definition execution) or that 
somelist*n copies the contents.  Id() can help elucidate the rules even if 
it is not so useful in running code.

The difference between mutables and immutables is that the interpreter may 
optionally not create a new immutable when it would have created a new 
mutable, but may instead return a reference to an existing immutable of the 
same value.  I call this an application of the 'as if' rule because (except 
for calls to id() and use of 'is') the future behavior of the program is 
the same as if the interpreter has created the new immutable.  (The other 
exception to 'the same' is that the program may run instead of crash due 
the the space saving. ;-)

tjr






More information about the Python-list mailing list