Skip to content
๐Ÿง  Mutability and Assignment

๐Ÿง  Mutability and Assignment

Understanding mutability is essential for predicting how Python handles assignment, modification, and shared references.


๐Ÿ”ข Definitions

  • Immutable Types: Cannot be changed after creation.
    • Examples: int, float, str, tuple, frozenset, bool
  • Mutable Types: Can be changed in place.
    • Examples: list, dict, set

๐ŸŸข Mutable Behavior โ€” Shared References

Mutable objects retain identity across assignments.

list_a = [1, 2, 3]
list_b = list_a  # Both refer to the same object

list_b.append(4)
list_b[0] = 99

print(list_a)  # Output: [99, 2, 3, 4]

๐Ÿ”ด Immutable Behavior โ€” New Object on Change

Immutable objects cannot be changed in place. Any “modification” creates a new object.

String Example

str_a = "hello"
str_b = str_a

str_b = "world"

print(str_a)  # hello
print(str_b)  # world

Integer Example

x = 5
y = x

x = 6

print(x, y)  # 6, 5

๐Ÿ”— Variable Assignment = Name Binding

  • a = b = 0 โ†’ both a and b refer to the same object (0)
  • Reassignment (a = a + 1) creates a new object for a
  • Mutation (a += [2]) may modify the object in place

๐Ÿง  Addition and Equality Notation

There are 2 ways to modify a variable through the addition and equality notation: a += i

a = a + i

โš ๏ธ Immutable Types

Let a = 0

To increment, we do: a += 1

It looks like the object is changing value in-place.

But it is actually doing a = a + 1, as for immutable types, a new object in memory has to be created for a to reference

The changing of an object in memory using the same reference is called rebinding

๐Ÿ’ก
Conclusion
Thus, there is no difference between a += 1 and a = a + 1 for immutable types.

In this case, a += 1 is syntactic sugar

โœ… Mutable Types

Let a = [1]

To add new value, we do: a += [2]

This does in fact modify the list in place a = [1, 2] <———– Memory A

Which means the object in the same memory also changes


With the same start a = [1]

If we did this instead: a = a + [2]

We will create a new object that now the same reference, a, newly refers to in memory

a = [1, 2] <———– Memory B

Thus, for mutable types there is a difference between a += 1 and a = a + 1

โœ… Best Practices

TypeUse a += i when…Use a = a + i when…
ImmutableYou want clean, idiomatic codeYou want to emphasize rebinding
MutableYou intend to mutate in placeYou want a new object, avoid side effects

In order to standardise the use of which for which, I would use a = a + 1 strictly for immutable types

Last updated on