Skip to content
Snippets Groups Projects
Commit 4d17374f authored by Blaise Li's avatar Blaise Li
Browse files

Move comment to the correct place.

parent 561097d6
Branches
No related tags found
No related merge requests found
Pipeline #58079 passed
...@@ -47,25 +47,22 @@ Exercise ...@@ -47,25 +47,22 @@ Exercise
>>> id(x) >>> id(x)
139950507563632 139950507563632
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
``[1, 2, 3, 4]``.
Exercise Exercise
-------- --------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment