Augmented Assignment (Sets)
00:00 Let’s take a deep dive into how augmented assignment actually works. You saw that many of the modifying set methods have a corresponding augmented assignment.
00:09
Like we saw, intersection update (&=
), and difference update (-=
), and symmetric difference update (^=
). These are not the same as their expanded out counterparts.
00:19
For example, x &= {1}
is not the same as x = x & {1}
.
00:29
Here’s an example where you will see that. Here we have x = {1}
, y = x
. That will make y
point to the same set that x
is pointing to.
00:41
Now we will use update (|=
) to update x
with {2}
.
00:47
Here, x
becomes {1, 2}
and y
also becomes {1, 2}
because they are pointing to the same object. Versus x = {1}
, y = x
, x = x | {2}
. x
will become {1, 2}
, but y
will become nothing.
01:08
It will actually be the original {1}
. This is because x = x | {2}
created a new set of {1, 2}
and then bound that back to x
. y
still pointed to the original {1}
.
01:25
The first example mutates x
, the second example reassigns x
.
Become a Member to join the conversation.