I came across the symbol ":=" in a Python snippet. Apparently it assigns to a variable, when calling a function.
import numpy
numpy.random.shuffle( x := [1, 2, 3, 4] )
# x is now created/overwritten
Is there any documentation on this ":=" symbol?
I came across the symbol ":=" in a Python snippet. Apparently it assigns to a variable, when calling a function.
import numpy
numpy.random.shuffle( x := [1, 2, 3, 4] )
# x is now created/overwritten
Is there any documentation on this ":=" symbol?
This is called the "walrus operator" and it's used for assignment expressions. It's new in Python 3.8.
This is frankly not a good use of it since all it does is save writing x again, while making the code confusing (as you noticed, lol), and incompatible with Python 3.7-. Rewrite:
import numpy
x = [1, 2, 3, 4]
numpy.random.shuffle(x)
See PEP 572 for more details and better examples, especially this one.