[Go to site: main page, start]

Mutating with an assignment statement PREMIUM

In Python, you can use an assignment statement to mutate an object. Assigning to an attribute, index, or key will mutate the object that contains that attribute, index, or key.

Trey Hunner Trey Hunner 2 min read 02:17 video Python 3.10—3.14
Watch this as a 02:17 screencast
Same content, narrated. Your preference is remembered for next time.
Watch it

In Python, you can use an assignment statement to mutate an object.

Assigning to an index will mutate the object you're indexing

Normally when we think of assignment statements we think of assigning a variable name to a value, like this:

>>> numbers = [2, 1, 3, 4, 7, 11, 18]

We've pointed the variable numbers to a new list of numbers.

>>> numbers
[2, 1, 3, 4, 7, 11, 18]

But variables aren't the only thing we can assign to.

For example, we could assign to the first index (index 0) in this list:

numbers[0] = 40

That assignment statement mutated our list:

>>> numbers
[40, 1, 3, 4, 7, 11, 18]

When you assign to an index in a list, you mutate that list.

Assigning to an attribute mutates also

Assigning to an attribute will mutate the object which contains that attribute.

We have class called Point:

class Point:

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def __repr__(self):
        return f"Point({self.x}, {self.y}, {self.z})"

And we have an instance of this Point class:

>>> p = Point(1, 2, 3)
>>> p
Point(1, 2, 3)

If we assign to the x attribute of our Point object:

>>> p.x = 40

We'll mutate that Point object:

>>> p
Point(40, 2, 3)

You can see this by stepping through this Python Tutor visualization:

Assigning an index to a list mutates that list. Assigning a key in a dictionary mutates that dictionary. And assigning an attribute on an object mutates that object.

So whenever we assign to:

  1. An index in a list
  2. A key in a dictionary
  3. An attribute on an object

we'll mutate that list, dictionary, or object.

Pointers always reference to an object

Regardless of whether we're assigning to a variable, attribute, index, or key, assignment statements always point our assignment target to a value.

Let's take a new list and point the variable colors to it:

>>> colors = []

And let's assign p.colors (the colors attribute on our Point object) to this new colors list:

>>> p.colors = colors

If we then we mutated the list that colors points to (by appending an item to it):

>>> colors.append('pink')

The colors attribute on our Point object:

>>> p.colors
['pink']

Even though we didn't change list p.colors directly.

This happens because p.colors points to the same list as the colors variable.

You can see this change by stepping through this interactive Python Tutor visualization:

Assignments always point something to a value

Attributes point to values the same way variables point to values. Similarly, list indices and dictionary keys point to values as well.

An assignment statement (the = sign) always points something to a value; it could be a variable, index, key, or attribute, but wherever you see an assignment something is going to be pointed to a value (a.k.a. object).

Summary

Assigning to an attribute, index, or key will mutate the object that contains that attribute, index, or key.

Next up
Augmented assignments mutate
03:11 watch

When possible, augmented assignments (in-place addition for example) operate "in place", meaning they do both an assignment and a mutation at the same time (except with immutable objects).

Watch
This is a free preview of a premium screencast. You have 2 previews remaining.