Python Reference ยท Practical Pages
Mutable and immutable types in Python
Concept page on Python mutability: which built-in types can change in place, how aliasing and default arguments bite, and why tuples can hold mutable items.
An object is mutable if its contents can change after it is created, and immutable if they cannot. The distinction decides what happens when two names refer to the same object: with an immutable object nobody can change it behind your back; with a mutable one, every name sees every change.
Immutable does not mean the name is fixed. You can always rebind a name to a new object; what immutability forbids is changing the object itself.
s = "abc" s += "d" # creates a NEW string and rebinds s nums = [1, 2] nums += [3] # extends the SAME list in place
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Built-in types
| Immutable | Mutable |
|---|---|
int, float, complex, bool |
list |
str, bytes |
dict |
tuple, range |
set, bytearray |
frozenset, None |
most instances of your own classes |
Instances of user-defined classes are mutable unless you take steps: @dataclass(frozen=True), __slots__ with no setters, or properties without setters.
Usage notes
- Hashing. Only hashable objects can be dict keys or set members. Immutable built-ins are hashable (a tuple only if all its items are); mutable containers are not. Use a tuple or
frozensetwhen you need a composite key. - Methods that mutate return
None.list.sort(),list.append(),dict.update()andset.add()change the object and returnNone, sox = x.sort()destroys your data.sorted()returns a new list. +=depends on the type. It calls__iadd__when there is one (lists mutate in place), otherwise it falls back toa = a + b(tuples, strings and numbers build new objects).- Default arguments are evaluated once, at function definition time. Use
Noneas the default for any mutable default. - Strings in loops. Repeated
s += piecemay be slow for long inputs; collect pieces in a list and call"".join(parts). - Immutability is shallow. A tuple only guarantees that it will always refer to the same items. See shallow and deep copies.
- Safe sharing. Pass immutable values freely; copy mutable ones (
list(x),x.copy()) when a function must not change the caller's data.
Related
- tuple, list, frozenset
- Shallow and deep copies, The Python data model
- hash()
- Tutorial lessons: Lists, Tuples, Functions