Tuples and immutability
greg.ewing at canterbury.ac.nz
Tue Mar 11 06:03:10 CET 2014
Ian Kelly wrote:
> It's technically "possible" for this augmented assignment to be
> performed in place:
> x = 12
> x += 4
> But it's not done in-place, because ints are meant to be immutable.
Which means it's *not* possible, because doing so
would violate the documented properties of the int
> In any case, this means that whether the operation is actually
> performed in-place is an implementation detail
The implementation could theoretically perform this
optimisation if there are no other references to the
object. But this will be completely invisible, because
to even find out whether it's the same object, you need
to keep another reference to the original object,
preventing the optimisation from being performed.
As far as observable effects are concerned, it's
quite clear: mutable objects can *always* be updated
in-place, and immutable objects can *never* be.
More information about the Python-list