Featurecore
Assignment expressions
Also called walrus operator, colon equals operator, named expression, PEP 572, assign inside an if, assign in a while condition.
name := expr assigns a value to a name and also returns the value, so an if, a while or a comprehension can test a value and keep it. The target is a single name in the current scope.
- Language
- Python
Synopsis
chunks = iter(["ab", "cd", ""])
while chunk := next(chunks):
print(chunk)
print(chunk == "")
ab
cd
True
The condition reads a chunk, binds it to chunk and tests it. The loop stops at the empty string.
Semantics
r1. "An assignment expression (sometimes also called a “named expression” or “walrus”) assigns an expression to an identifier, while also returning the value of the expression."1 Example: while-loop.
r2. "Assignment expressions must be surrounded by parentheses when used as expression statements and when used as sub-expressions in slicing, conditional, lambda, keyword-argument, and comprehension-if expressions and in assert, with, and assignment statements." "In all other places where they can be used, parentheses are not required, including in if and while statements."1 Examples: parentheses, bare-statement.
r3. "An assignment expression does not introduce a new scope." "it is the current scope. If this scope contains a nonlocal or global declaration for the target, the assignment expression honors that."2 Example: scope.
r4. In a comprehension, an assignment expression "binds the target in the containing scope".2 So the name is still bound after the comprehension. Example: comprehension.
r5. "assignment expressions don't support the advanced features found in assignment statements". "Single assignment targets other than a single NAME are not supported", and unpacking, inline type annotations and augmented assignment are not supported.2 Example: attribute-target.
r6. "Priority around commas is different": x = 1, 2 sets x to (1, 2), but (x := 1, 2) sets x to 1.2 Example: commas.
Examples
import re
data = "id=42"
if m := re.search(r"\d+", data):
print(m.group())
values = [y := 5, y ** 2]
print(values, y)
42
[5, 25] 5
In an if statement and in a list display, no parentheses are needed (rule r2).
data = [3, 8, 12]
big = [y for x in data if (y := x * 2) > 10]
print(big, y)
print(any((hit := n) > 5 for n in data), hit)
[16, 24] 24
True 8
y and hit stay bound after the comprehension. hit keeps the first value that made any() stop (rule r4).
def count():
total = 0
def add(n):
nonlocal total
return (total := total + n)
add(2)
add(3)
return total
print(count())
5
x = 1, 2
print(x)
(x := 1, 2)
print(x)
(1, 2)
1
This code raises SyntaxError, on purpose.
class C:
pass
c = C()
if (c.value := 3) > 0:
print(c.value)
Pitfalls
An assignment expression cannot be a statement on its own without parentheses:
This code raises SyntaxError, on purpose.
y := 3
PEP 572 says that this rule makes the choice between the two forms simple, and that (y := f(x)) is "Valid, though not recommended".2 Use a normal assignment statement.
In the grammar, the right side of := is a whole expression (see the grammar of this page).1 n := len(a) > 3 binds n to the result of the comparison. Put parentheses around the assignment expression when another operator follows it:
This code runs but gives a wrong answer on purpose.
a = [1, 2, 3, 4]
if n := len(a) > 3:
print(n)
True
Write if (n := len(a)) > 3: to bind n to 4.
Comparison
In the Go grammar, an assignment is a statement: "SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl". An if statement can start with a simple statement: "The expression may be preceded by a simple statement, which executes before the expression is evaluated", as in if x := f(); x < y { ... }.3 Each if, for and switch statement "is considered to be in its own implicit block", so x is local to the if statement.3 A Python name bound by := in an if stays bound in the current scope after the statement (rule r3).
References
- The Python Language Reference, Python 3.15, 6.12. Assignment expressions
- PEP 572 - Assignment Expressions, Scope of the target; Exceptional cases; Differences between assignment expressions and assignment statements
- The Go Programming Language Specification, If statements; Blocks; Statements
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 7WX5QR Edit this page History Report an error Markdown