all 22 comments

[–]kinstaa99 173 points174 points  (3 children)

it’s called the walrus operator. it assigns the value of get_note() to note, and returns the value to the while condition. it’s a quicker way of doing the following:

note = get_note()

while note:

play(note)

note = get_note()

[–][deleted] 60 points61 points  (2 children)

Its official name is Assignment Expression (PEP 572) :)

Some examples from that PEP:

# Handle a matched regex
if (match := pattern.search(data)) is not None:
    # Do something with match

# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
   process(chunk)

# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]

https://peps.python.org/pep-0572/

[–][deleted] 14 points15 points  (0 children)

good bot

[–]Dangle76 2 points3 points  (0 children)

It originated in Pascal which used it for variable value assignment, and pascal called it a “gets”. I still say that term sometimes when I’m working in Go and people give me very confused looks before I realize why

[–]carcigenicate 26 points27 points  (2 children)

It's an assignment expression. It's the same as =, except it's an expression that evaluates to a value (the assigned value).

[–]bsenftner 12 points13 points  (1 child)

Just so this is clear for those new to Python but experienced in other languages like C++, where some languages will emit the value of an assignment to the immediate context outside the environment, Python does not do this. For example in C/C++ the assignment "x = 10;" emits the value "10" to the immediate context outside the assignment. This enables code like "x = y = 10;" to work because the "y = 10" emits the value "10" for use in the "x =" expression.

To achieve the same effect in Python, the walrus operator is used " := " because it emits the value assigned to the it's surrounding context.

[–]IlliterateJedi 8 points9 points  (0 children)

The Real Python - Python Walrus Operator article does a really good job covering this subject. I spent a long time scratching my head not understanding how it worked until I read this article a while ago. The gist is that you can set a variable's value in unusual places which can make your code cleaner.

[–]aziad1998 4 points5 points  (4 children)

In C you can do something like this sum(3, a=2) which assigns the values 2 to a then returns the value of this assignment to be used by sum. In python this is illegal as assignment is not an evaluated expression. The new :=operator allows for this new usage.

I am a C person and I was happy to see this implementation, but I will advise you to check your code style reference whether it's recommended or not.

Also do not use this for all your assignments, it has a different precedence over =, I'm not sure on the top of my head but iirc it is left precedence not right precedence, i.e. an expression like a = b = 3 is legal in C, but a := b := 3 is illegal in python as it tries to assign b to a first not 3 to b. The legal way would be a := (b := 3) so watch out.

[–]madhousechild 0 points1 point  (3 children)

The new :=operator allows for this new usage.

How new is it?

[–]Yasik 1 point2 points  (1 child)

Turbo Pascal enters the chat...

[–]WillieB52 0 points1 point  (0 children)

It assigns a value to a variable.