list comprehension problem

Scott David Daniels Scott.Daniels at Acm.Org
Tue Nov 3 10:04:41 EST 2009


Terry Reedy wrote:
> What immutability has to do with identity is that 'two' immutable 
> objects with the same value *may* actually be the same object, 
> *depending on the particular version of a particular implementation*.
> 
>>
>>>>> t1 = (1,2,3) # an immutable object
>>>>> t2 = (1,2,3) # another immutable object
> 
> Whether or not this is 'another' object or the same object is irrelevant 
> for all purposes except identity checking. It is completely up to the 
> interpreter.
> 
>>>>> t1 is t2
>> False
> 
> In this case, but it could have been True.
> 
>>>>> t1 == t2
>> True

A more telling example:
 >>> t1 = (1, 2) + (3,) # an immutable object
 >>> t2 = (1,) + (2, 3) # another immutable object
 >>> t1 is t2
 >> False
 >>>>> t1 is t2
 >> False

Here you make obvious that (assuming an optimizer that
is not far more aggressive than Python is used to), in
order to make equal immutable values identical, you'd
have to end each operation producing an immutable result
with a search of all appropriately typed values for one
that was equal.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list