DEV Community

Julien AWON'GA
Julien AWON'GA

Posted on

Walrus Operator in Python

What is the Walrus Operator?

The Walrus Operator, also known as the Assignment Expression, is a new operator in Python 3.8 that allows you to assign a value to a variable as part of an expression. The syntax of the operator is :=, and it can be used in any expression where you would normally use an assignment statement.

For example, let's say you want to read a line from a file and print it only if the line contains the word "Python". In pre-3.8 Python, you would write something like this:

with open('file.txt') as f:
    line = f.readline()
    if 'Python' in line:
        print(line)
Enter fullscreen mode Exit fullscreen mode

With the Walrus Operator, you can simplify this code by combining the readline() method and the if statement into a single expression:

with open('file.txt') as f:
    if (line := f.readline()) and 'Python' in line:
        print(line)
Enter fullscreen mode Exit fullscreen mode

Here, the Walrus Operator assigns the result of f.readline() to the variable line and also checks if line contains the word "Python". If both conditions are true, the line is printed.

How to Use the Walrus Operator

The Walrus Operator can be used in any expression that requires an assignment statement. Some common use cases include:

Loop conditions: You can use the Walrus Operator to simplify loop conditions. For example, instead of writing while True: and then checking for a condition inside the loop, you can write while (x := get_next_value()): and assign the next value to x within the condition.

List comprehensions: You can use the Walrus Operator in list comprehensions to simplify the code. For example, instead of writing [x for x in some_list if some_condition(x)], you can write [x for x in some_list if (y := some_condition(x))] and use y later in the comprehension.

Function calls: You can use the Walrus Operator in function calls to simplify the code. For example, instead of writing result = some_function() and then checking if result is not None, you can write if (result := some_function()) is not None: and assign the result to result within the if statement.

Caveats and Best Practices

While the Walrus Operator can be very useful, there are some caveats and best practices that you should keep in mind:

  • Don't overuse it: While the Walrus Operator can make your code more concise, overusing it can make your code less readable and harder to maintain. Use it only when it makes sense and improves the code.

  • Don't use it in complex expressions: The Walrus Operator should only be used in simple expressions. Using it in complex expressions can make the code harder to read and understand.

  • Don't use it for side effects: The Walrus Operator should only be used for assigning values to variables. Using it for side effects (e.g., opening a file) can make the code harder to read and understand.

  • Be aware of operator precedence: The Walrus Operator has a lower precedence than most other operators, so be careful when using it in expressions with other operators.

The Walrus Operator is a useful addition to Python 3.8 that can simplify your code and make it more concise. It allows you to assign values to variables within expressions, which can be very handy in many situations. However, you should be careful not to overuse it and follow best practices when using it.

Top comments (0)