Clarification on Immutability please
Am 27.01.2020 15:23 schrieb Chris Angelico:
> The way execution works in Python, you first evaluate the object, then
> assign it to the target. The new object HAS to exist before the old
> one is replaced. There's no such thing as "atomic reassignment" that
> simultaneously destroys the old object and assigns a new one. The
> slicing will always happen first, and then the assignment.
I agree that that is how it's done because it makes sense. My completely
academic question is this: If the Python compiler sees that the
operation effectively just chops a few items off the end of a tuple
which will be immediately discarded I can't see an issue with an
implementation simply shortening the length of the tuple. In C this
would be an ultra cheap operation, and the pointer to the tuple object
(CPython's object ID) would indeed not change. A possible drawback would
be waste of memory because the unused tuple items are still around in
The introductory comment in tupleobject.h is clear on this subject:
"[...] C code can change the tuple items (but not their number) [...]",
so this is not how it's done in CPython 3.8.0, but IMO an implementation
could legally do this.
All this is beside the point of the OP's question. There is no
connection between an object's mutability and its ID.