Clarification on Immutability please

Musbur musbur at posteo.org
Mon Jan 27 09:05:37 EST 2020


Am 21.01.2020 19:38 schrieb Chris Angelico:
> On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker <stephen_tucker at sil.org> 
> wrote:

>> and even that the first id(mytup) returns the same address as the 
>> second
>> one, I am left wondering exactly what immutability is.

Let's look at id()'s documentation:

  id(object)

     Return the “identity” of an object. This is an integer which is 
guaranteed to be unique and constant for this object during its 
lifetime. Two objects with non-overlapping lifetimes may have the same 
id() value.

> Are you sure that it does? I can't reproduce this. When you slice the
> first two from a tuple, you create a new tuple, and until the
> assignment happens, both the new one and the original coexist, which
> means they MUST have unique IDs.

I'd expect that, too, but an "atomic" reassignment would not contradict 
the documentation.

>> Somehow, it seems, tuples can be reduced in length (from the far end)
>> (which is not what I was expecting), but they cannot be extended 
>> (which I
>> can understand).

Different ID means different object, but identical ID doesn't mean 
identical object. The Python implementation allows re-use of an object's 
ID after the object has been destroyed, and the documentation mentions 
this explicitly.


More information about the Python-list mailing list