list comprehension problem

Terry Reedy tjreedy at udel.edu
Sat Oct 31 19:12:40 CET 2009


alex23 wrote:
> Terry Reedy <tjre... at udel.edu> wrote:
>> alex23 wrote:
>>> You're completely wrong. Immutability has nothing to do with identity,
...
 > I'm honestly not getting your point here.

Let me try again, a bit differently.

I claim that the second statement, and therefor the first, can be seen 
as wrong. I also claim that (Python) programmers need to understand why.

In mathematics, we generally have immutable values whose 'identity' is 
their value. There is, for example, only one, immutable, empty set.

In informatics, and in particular in Python, in order to have 
mutability, we have objects with value and an identity that is separate 
from their value.  There can be, for example, multiple mutable empty 
sets. Identity is important because we must care about which empty set 
we add things to. 'Identity' is only needed because of 'mutability', so 
it is mistaken to say they have nothing to do with each other.

Ideally, from both a conceptual and space efficiency view, an 
implementation would allow only one copy for each value of immutable 
classes. This is what new programmers assume when they blithely use 'is' 
instead of '==' (as would usually be correct in math).

However, for time efficiency reasons, there is no unique copy guarantee, 
so one must use '==' instead of 'is', except in those few cases where 
there is a unique copy guarantee, either by the language spec or by 
one's own design, when one must use 'is' and not '=='.  Here 'must' 
means 'must to be generally assured of program correctness as intended'.

We obviously agree on this guideline.

Terry Jan Reedy




More information about the Python-list mailing list