Try it here
Subscribe
The Walrus Operator

Python new Walrus operator := in 3.8

python_new_walrus_operator_:=_in_3.8

In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.

Python 3.8 introduced the use of ‘colon equals’ (:=), which is similar to the equals operator (=). The use of this operator allows for speedup and shortened code.

In the most recent version of Python 3.8 has emerged the accepted use of :=, or the ‘walrus operator’. It used for defining a variable within an expression in a programming context.

With the function f defined as f = lambda x : x+2, which simply adds two to any input, consider the following code:

>>> f = lambda x: x + 2
>>> data = [1, 2, 3, 4]
>>> f_data = [y for x in data if (y := f(x)) % 2 == 0]
>>> f_data
[4, 6]
>>> 

This is a much more efficient implementation than the alternative, which runs an input through the function twice:

>>> f_data = [f(x) for x in data if f(x) % 2 == 0]
>>> f_data
[4, 6]
>>> 

The following construction of a list g is completely valid using the walrus operator:

>>> g = [y := f(3), y**2, y **3] # The location of y := f(3) can be anywhere in the list
>>> g
[5, 25, 125]
>>> 

y is being defined as f(3), and it is being used immediately after, in the same expression. We could not write something like g = [y=f(3), …], since the assignment of variables using = must be done on its own line and not within another expression.

This could alternatively be written with two lines using the standard equals operator:

>>> y = f(3)
>>> g = [y, y**2, y**3]
>>> g
[5, 25, 125]
>>> 

So,

:= can be used to assign variables while another expression is being evaluated.

In many ways, the := operator is similar with the defining of placeholder variables, such as for i in range(x):, in which a variable i is initialized within the expression of the for loop. It can be thought of as an extension or generalization of these ‘hidden variable initializations’.

The walrus operator cannot be used for everything, however. The following statements are illegal:

a := 3 # must be done with a=3.
a = b := 4 # must be done with a=b=4
a = (b := 4) # legal, but not recommended

It’s worthwhile to explore why the expression a=(b:=4) works. This lies in the realization that b:=4 not only sets b equal to four but returns its value. This is also the reason why the list comprehension statement above (if y := f(x)) % 2 ==0]) works. This statement evaluates b:=4 as an expression, which returns a value. The result: a and b are both set to 4.

Taking this further, simply putting parenthesis around the first statement like such — (a := 3) — runs properly, because using parenthesis marks everything inside it as an expression. Walrus operators can only be used inside other expressions.

As another example of code bordering on unpythonic, consider the following completely legal function.

>>> f = lambda x : (m := x + 1) + (m ** 2)
>>> f(2)
12
>>> 

Key Points

  • The walrus operator is denoted :=, and introduced in Python 3.8.
  • This operator is used for and only for the assignment of variables within another expression. At the very least, it can save one or more lines of code, and at most, it can dramatically speed up the processing of large data.
  • Walrus operators can be used everywhere from loops to functions to list comprehension to if statements to roundabout variable assignment.

For additional details and examples about walrus operator, read PEP page here.

Writer profile pic

Abhijit on Sep 06, 2020 at 10:09 am


This article is contributed by Abhijit. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.