The Walrus Operator — The Best Python Feature You Don’t Know

SamCoding
3 min readNov 7, 2022

--

Python’s Most Controversial Feature

A laptop by a window in the evening, beside a lightbulb.
Photo by Oskar Yildiz on Unsplash

The walrus operator is a feature of the Python programming language introduced in Python 3.8, released on the 14th of October 2019, and sparked immediate controversy amongst the world of Python developers. Known as the “Walrus”, its name comes from the friendly walrus face it seems to resemble.

:=

How To Use the Walrus Operator

The walrus operator is quite simple, but sometimes hard to get your head around at first. It assigns a value to a variable, whilst simultaneously returning that value.

# The old way
x = 1
print(x) # 1
print(x) # You can reuse the variable
# With the walrus operator
print((x:=1)) # 1
print(x) # You can reuse the variable here too!

The Problems

One of the most important issues that arose was that some claimed it violated the principles Python was developed by, nicely summed up in the “Zen of Python”.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

The idea that the language should be elegantly simple and easy to read and learn was not fitting, said some, to the walrus operator, which is not clear what it does immediately. The obvious lines it defied are “Readability counts.”, and arguably “Beautiful is better than ugly.”.

Why I Like It

Although controversial, I have to say I like it! I think it speeds up writing some algorithms, as seen below, often concerning for or while loop iteration.

Iteration can be improved. For example, consider the below code block, where a randomly fluctuating line v is generated.

import numpy as np
v = [0]
for _ in range(100):
v.append(v[-1]+np.random.randn())

This can be improved to a shorter snippet as below.

import numpy as np
x = 0
v = [(x := x+np.random.randn()) for _ in range(100)]

In my opinion, much cleaner! There are many other uses for this, but the above is my favourite.

Conclusion

I know it may be controversial, and there are bound to be readers who don’t agree with my opinion, but I think the Walrus operator is a slightly unnecessary, good addition to Python that will hopefully be here to stay.

--

--

SamCoding
0 Followers

A student, machine learning engineer, musician and developer.