+ 4
Walrus Operator
What is walrus operator in python3?
1 Answer
+ 5
:=
It's one of the new features of python (3.8 for testing tho... )
"...since := is an expression, it can be used in contexts where statements are illegal, including lambda functions and comprehensions..." *
i.e. It allows to assign a value to a variable in a condition*:
# python 3.7
m = pattern.search(w))
If m is not None:
process(m)
# using :=
if (m := pattern.search(w)) is not None:
process(m)
*https://www.python.org/dev/peps/pep-0572/#differences-between-assignment-expressions-and-assignment-statements