DEV Community

Cover image for The Walrus Operator
Felix
Felix

Posted on

The Walrus Operator

Python is a programming language that stands out for its simplicity and readability. It allows developers to express complex ideas in clear and succinct ways. The introduction of the “walrus operator” ( := ) in Python 3.8 has added to the efficient and readable nature of Python’s syntax.

What is the Walrus Operator

The walrus operator is an assignment expression. This means that it allows you to assign a value to a variable within an expression. The operator got its name due to its resemblance to the eyes and tusks of a walrus on its side.

Typically, when you want to assign a value to a variable you do something like this:

x = 5
print(x)
# 5
Enter fullscreen mode Exit fullscreen mode

In this example, we use the traditional method of assigning a value to a variable by using the ' = ' sign. Then, we use the print statement to display the value of that variable. With the walrus operator, however, we can complete both of these steps at once.

print(x := 5)
# 5
Enter fullscreen mode Exit fullscreen mode

Here, the assignment expression allows you to assign the value of 5 to the variable, and immediately print the value. The walrus operator can also be useful in more complex expressions, like while loops. First, we'll look at an example that doesn't include the operator.

names= []
while True:
    name = input("Type in a name: ")
    if name == "quit":
        break
    names.append(name)
Enter fullscreen mode Exit fullscreen mode

Here, we create an empty list and store it in the variable called 'names'. Then, we ask the user to type in a name and store the user's input into a variable called 'name'. If the user types the word 'quit', then the while loop will be terminated. For any input other than the word 'quit', the input gets stored in the 'names' list. Using the walrus operator, this block of code can be shortened.

names = []
while (name := input("Type in a name: ")) != "quit":
    names.append(name)
Enter fullscreen mode Exit fullscreen mode

In this example, we assign the value of the user input to the variable ‘name’ and simultaneously check to see if 'name' is equal to the word ‘quit’. If it isn’t, then the statement evaluates to True and the name is added to the list. If it is equal to ‘quit’ then the statement evaluates to false and the while loop is terminated.

While the walrus operator makes the block of code above more succinct, it also takes a bit more effort to read it properly and figure out what that line of code is doing. For this reason, there may be occasions where the walrus operator may not be the best method to use in order to make your code more readable.

Altogether, using the walrus operator is a good method for making your code more efficient. While it might not be suitable for all scenarios, the operator is a good way reduce code length and enhance readability.

Top comments (3)

Collapse
 
thormeier profile image
Pascal Thormeier

First of all, thanks for sharing, I didn't know about this operator! Your practical example shows really well how it can condense functionality. However, I'm a bit torn about the operator itself. On one hand, it allows us to condense code down, make it shorter, perhaps even get a little performance boost. On the other hand, this happens at the cost of readability. Your while example shows this really well: This single line of code does a lot at once. Junior devs might be intimidated by it, and people not aware of the operator might even think it's a typo. I'm not sure I'll ever actively use it, but if I stumble upon it now, I'll at least know now what it does.

Collapse
 
flaniyanjr profile image
Felix

Yea I completely understand where you're coming from. Being new to coding myself, using the operator doesn't come naturally to me yet. I've begun to start using it when I refactor my code, hoping that the more I do this the more natural it'll become in the future.

Collapse
 
sreno77 profile image
Scott Reno

That's an easy way to write more concise code. Thanks for sharing!