..

Python's Walrus Operator

What is the Walrus Operator?

The walrus operator was added to Python in version 3.8, when PEP 572 was accepted. The official name for the := syntax is “assignment expression operator,” but for this article, I will call it the walrus operator.

The goal of the walrus operator := is to allow users to name variables and use them in expressions with ease. It takes the form of NAME := expr. This syntax is used in many different languages, for example, in Golang. Below is an example in Python:

while data := file.read(1024):
    process(data)

In this example, each time the while loop runs, data will be set to the output of file.read(1024) until there is no data left to read. Each loop will run the process function on each 1024-byte chunk of data. While this is a simple example, it shows that you can assign the output of an expression file.read(1024) to a variable in the while expression. This looks a lot simpler than the alternative way of writing this:

data = file.read(1024)
while data:
    process(data)
    data = file.read(1024)

Note that here the walrus operator is not only more concise but also more readable. This is because the assignment is done in the while loop itself, so you can see that the data variable is only used in the while loop. I believe that this potentially might help with garbage collection as well since the scope of the data variable is limited to the while loop. However, I recommend you do your own testing to confirm this.

This operator also allows a user to reuse an expression in the case of regex matching.

import re
pattern = re.compile("foo")
if (match := pattern.search("This is just foobar")) is not None:
    print(match.group())

We are saving one line of code by using the walrus operator, as you can see here. The alternative way of writing this without using the new walrus operator is as follows:

import re
pattern = re.compile("foo")
match = pattern.search("This is just foobar")
if match is not None:
    print(match.group())

Obviously, in real-world examples, you would be doing more than just printing. But this is a simple example to show how the walrus operator can be used to make your code more concise.

Conclusion

I believe that the walrus operator is a great addition to the Python language and is a well-known feature in other languages. It allows for more concise code and can make your code easier to read and understand. However, if you are not aware of the syntax or have not seen anything similar in other languages, it can be a little confusing. I recommend only using it when it makes sense; if you need to write a long comment to explain what it does, then it might not be the best choice.