Search code examples
pythonmutability

Understanding mutability in Python


I have this snippet:

a = [1,2,3]
b = a
b = [4,5,6] # a doesnt change

and this:

a = [1,2,3]
b = a
b[0] = 5 # a also changes

How is b's initialization playing a part in deciding the mutability of a?


Solution

  • When you create a list and assign it to a variable, like

    a = [1, 2, 3]
    

    You create an object, and the variable a holds a reference to that object. When you do

    b = a
    

    You assign the same reference in b, so now both a and b point to your list. So when you execute

    b[0] = 5
    

    You alter that same list.

    You can see this in action by using the id() function, which returns the memory address of the object:

    >>> a = [1, 2, 3]
    >>> b = a
    >>> id(a), id(b)
    (140454191340720, 140454191340720)
    

    They are identical. The point is, a and b are not lists themselves, they point to a list.

    When you do something like:

    a = [1, 2, 3]
    b = a
    b = [2, 3, 4]
    

    You first assigned to b a reference to the list that a points to, but then you assign a new reference to it.

    By the way, this can bite you in the behind when you do something along the lines of

    def foo (a=[]):
        a.append(42)
        return a
    

    since the argument a points to the same list on every invocation, if you call this function 5 times without arguments, the list will contain 5x 42:

    foo(); foo(); foo(); foo();
    print(foo())
    >>> [42, 42, 42, 42]