-1

Thank you for your valuable time, I have just started learning Python. I came across Mutable and Immutable objects. As far as I know mutable objects can be changed after their creation.

a = [1,2,3]
print(id(a))
45809352
a = [3,2,1]
print(id(a))
52402312

Then why id of the same list "a" gets changed when its values are changed.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
D3vil
  • 25
  • 4
  • 8
    Assignment isn't mutation. – Kevin Jan 31 '17 at 14:42
  • 1
    Possible duplicate of [Why two individually created immutable objects have same id and mutable objects have different while both refer to same values?](http://stackoverflow.com/questions/21091568/why-two-individually-created-immutable-objects-have-same-id-and-mutable-objects) – David Zemens Jan 31 '17 at 14:43
  • @Kevin thank you. Now I understand the concept of mutation. – D3vil Jan 31 '17 at 14:52

5 Answers5

5

your interpretation is incorrect.

When you assign a new list to a, you change its reference.

On the other hand you could do:

a[:] = [3,2,1]

and then the reference would not change.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

mutable means that the content of the object is changed. for example a.append(4) actually make a equal to [1, 2, 3, 4], while on the contrary, appending to a string (which is immutable) does not change it, it creates a new one.

However, when you re-assign, you create a new object and assign it to a, you don't alter the existing content of a. The previous content is lost (unless refered-to by some other variable)

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • 2
    You comparison to "appending to a string" is not so good, because a string does not have an `append` method. You can only do `string + "foo"`, which will create a new string, but so does `[1,2,3] + [4]`. `str.replace` might be a better example. – tobias_k Jan 31 '17 at 14:46
  • @tobias_k: ok, did not mean it literally, just wanted to convey the idea. In other languages there is an append, but still the main idea it the creation of a new object – blue_note Jan 31 '17 at 14:50
0

If you change a list, its id doesn't change. But you may do things that instead create a new list, and then it will also have a new id.

E.g.,

>>> l=[]
>>> id(l)
140228658969920
>>> l.append(3)  # Changes l
>>> l
[3]
>>> id(l)
140228658969920  # Same ID
>>> l = l + [4]  # Computes a new list that is the result of l + [4], assigns that
>>> l
[3, 4]
>>> id(l)
140228658977608  # ID changed
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
0

When you do

a = [3, 2, 1]
  1. You unlink the list of [1, 2, 3] from variable a.
  2. Create a new list [3, 2, 1] then assign it to a variable.
0

Being immutable doesn't mean you assign a new object, it means your original object can be changed "in place" for example via .append()

>>> my_list = [1,2,3]
>>> id(my_list)
140532335329544
>>> my_list.append(5)
>>> id(my_list)
140532335329544
>>> my_list[3] = 4
>>> my_list
[1, 2, 3, 4]
>>> id(my_list)
140532335329544
Seif
  • 1,058
  • 11
  • 19