With mutable object like ``list`` when we mutate the object the state of the object is modified.
With mutable object like ``list``, when we mutate the object, the state of the object is modified.
But the reference to the object is still unchanged.
But the reference to the object is still unchanged.
So in this example we have two ways to access to the list [1,2] if we modify the state of the list itself.
but not the references to this object, then the 2 variables x and y still reference the list containing
[1,2,3,4].
compare with the exercise on string and integers:
Comparison with the exercise on strings and integers:
Since list are mutable, when ``+=`` is used the original list object is modified, so no rebinding of *x* is necessary.
Since lists are mutable, when ``+=`` is used, the original list object is modified, so no rebinding of *x* is necessary.
We can observe this using *id()* which give the memory address of an object. This address does not change after the
We can observe this using *id()* which gives the memory address of an object. This address does not change after the
``+=`` operation.
``+=`` operation.
.. note::
.. note::
even the results is the same there is a subtelty to use augmented operator.
Even the results are the same, there is a subtelty to use augmented operator.
in ``a operator= b`` python looks up ``a`` ’s value only once, so it is potentially faster
In ``a operator= b`` opeeration, Python looks up ``a``'s value only once, so it is potentially faster
than the ``a = a operator b``.
than the ``a = a operator b`` operation.
compare ::
Compare ::
x = 3
x = 3
y = x
y = x
...
@@ -95,6 +92,10 @@ and ::
...
@@ -95,6 +92,10 @@ and ::
:figclass: align-center
:figclass: align-center
In this example we have two ways to access to the list ``[1, 2]``.
If we modify the state of the list itself, but not the references to this object, then the two variables ``x`` and ``y`` still reference the list containing