The world’s leading publication for data science, AI, and ML professionals.

Augmented Assignments in Python

Understanding how augmented assignment expressions work in Python and why you should be careful when using them with mutable objects

Photo by Kelly Sikkema on Unsplash
Photo by Kelly Sikkema on Unsplash

Introduction

One of the concepts that Python has borrowed from C language is the augmented assignment that essentially are shorthands for standard assignments.

In today’s short guide we will discuss what augmented assignments is all about and why you should be extra careful when using them for mutable data types.


Augmented assignments in Python

Augmented assignments create a shorthand that incorporates a binary expression to the actual assignment. For instance, the following two statements are equivalent:

a = a + b
a +=  b  # augmented assignment

In the code fragment below we present all augmented assignments allowed in Python language

a += b
a -= b
a *= b
a /= b
a //= b
a &= b
a |= b
a ^= b
a >>= b
a <<= b
a %= b
a **= b

Augmented assignments work on any data type supporting implied binary expressions.

>>> a = 1
>>> a = a + 2
>>> a
3
>>> a += 1
>>> a
4
>>> s = 'Hello'
>>> s = s + ' '
>>> s 
'Hello '
>>> s += 'World'
>>> s
'Hello World'

How augmented assignments work with mutable objects

When augmented assignment expressions are used, the most optimal operation will be automatically picked up. This means that for specific object types supporting in-place modifications, then an in-place operation will be applied since it is faster than first creating a copy and then the assignment. If you want to add an item to a list, is typically faster to use the append() method rather than using concatenation:

>>> my_lst = [1, 2, 3]
>>> my_lst.append(4)  # This is faster 
>>> my_lst = my_lst + [4]  # This is slower

And since in-place operations are faster, they will automatically be chosen in augmented assignments. To make it clear how these expressions work with mutable object types let’s consider the example below.

If we use concatenation, then a new list object will be created. This means that if another name is sharing the same reference, it will remain unaffected by any future updates in the new object list:

>>> lst_1 = [1, 2, 3]
>>> lst_2 = lst_1
>>> lst_1 = lst_1 + [4, 5]
>>> lst_1
[1, 2, 3, 4, 5]
>>> lst_2
[1, 2, 3]

Now if we use augmented assignment expression, which means that instead of concatenation the expression will use extend() method to perform this update, then we will also affect all other names holding the shared reference:

>>> lst_1 = [1, 2, 3]
>>> lst_2 = lst_1
>>> lst2 += [4, 5]
>>> lst_1
[1, 2, 3, 4, 5]
>>> lst_2
[1, 2, 3, 4, 5]

Three advantages of augmented assignments

To summarise, augmented assignment expressions offer the following three advantages:

  1. They require less key strokes, and are much cleaner
  2. They are faster since the left hand side of the expression needs to be evaluated only once. In most of the cases this may not be a huge advantage, but it certainly is when the left-hand side is a complex expression.
x += y  # x is evaluated only once
x = x + y  # x is evaluated twice
  1. The most optimal operation will be selected, meaning that if an object supports in-place changes, then in-place operation will be applied over concatenation. You should however be really careful as sometimes this may not be what you want since names sharing references may impact each other when in-place operations area applied.

Final Thoughts

In today’s article we discussed about augmented assignment expression in Python and how they can help you write more Pythonic, clear and even faster code.

Additionally, we highlighted how this type of expressions is applied over mutable data types that may cause us troubles with shared references.


Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Join Medium with my referral link – Giorgos Myrianthous


You may also like

What does if name == "main" do in Python?


Dynamic Typing in Python


Related Articles