Mutable vs Immutable
suggest changeThere are two kinds of types in Python. Immutable types and mutable types.
Immutables
An object of an immutable type cannot be changed. Item assignment raises TypeError. An operation such as += rebinds the name to a new object.
This category includes: integers, floats, complex, strings, bytes, tuples, ranges and frozensets.
To highlight this property, let’s play with the id builtin. This function returns the identity of the object passed as parameter. If the id is the same, this is the same object. If it changes, then this is another object. In CPython, id is the object’s memory address, and it is unique only while that object is alive. The numbers below are from another run.
>>> a = 1
>>> id(a)
140128142243264
>>> a += 2
>>> a
3
>>> id(a)
140128142243328
Okay, 1 is not 3… Breaking news… Maybe not. However, this behaviour is often forgotten when it comes to more complex types, especially strings.
>>> stack = "Overflow"
>>> stack
'Overflow'
>>> id(stack)
140128123955504
>>> stack += " rocks!"
>>> stack
'Overflow rocks!'
Aha! See? We can modify it!
>>> id(stack)
140128123911472
No. While it seems we can change the string named by the variable stack, what we actually do is create a new object to contain the result of the concatenation. We are fooled because in the process, the old object goes nowhere, so it is destroyed. In another situation, that would have been more obvious:
>>> stack = "Stack"
>>> stackoverflow = stack + "Overflow"
>>> id(stack)
140128069348184
>>> id(stackoverflow)
140128123911480
In this case it is clear that if we want to retain the first string, we need a copy. But is that so obvious for other types?
Exercise
Now, knowing how immutable types work, what would you say about the below piece of code? Is it wise?
s = ""
for i in range(1, 1000):
s += str(i)
s += ","
Mutables
An object of a mutable type can be changed, and it is changed in-situ. No implicit copies are done.
This category includes: lists, dictionaries, bytearrays and sets.
Let’s continue to play with our little id function.
>>> b = bytearray(b'Stack')
>>> b
bytearray(b'Stack')
>>> b = bytearray(b'Stack')
>>> id(b)
140128030688288
>>> b += b'Overflow'
>>> b
bytearray(b'StackOverflow')
>>> id(b)
140128030688288
(As a side note, I use bytes containing ascii data to make my point clear, but remember that bytes are not designed to hold textual data. May the force pardon me.)
What do we have? We create a bytearray and modify it. Using the id, we can ensure that this is the same object, modified. Not a copy of it. For these short values the id stayed the same; a bytearray can still reallocate and change id if it grows past its capacity.
Of course, if an object is going to be modified often, a mutable type does a much better job than an immutable type. Unfortunately, the reality of this property is often forgotten when it hurts the most.
>>> c = b
>>> c += b' rocks!'
>>> c
bytearray(b'StackOverflow rocks!')
Okay…
>>> b
bytearray(b'StackOverflow rocks!')
Waiiit a second…
>>> id(c) == id(b)
True
Indeed. c is not a copy of b. c is b.
Exercise
Now that you better understand the side effect of a mutable type, can you explain what is going wrong in this example?
>>> ll = [ [] ]*4 # Create a list of 4 lists to contain our results
>>> ll
[[], [], [], []]
>>> ll[0].append(23) # Add result 23 to first list
>>> ll
[[23], [23], [23], [23]]
>>> # Oops...