Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Numbers and Binary Data

The walrus operator :=

Reference for Python's walrus operator :=, added in 3.8: assign inside an expression in if, while and comprehensions, scoping rules, parentheses and style tips.

The walrus operator := is an assignment expression. It assigns a value to a name and also produces that value, so you can use it inside a larger expression. It was added in Python 3.8 (PEP 572).

The typical use is to avoid computing something twice or splitting a test and an assignment into separate lines: capture a regular-expression match while checking it, read a chunk in a while condition, or reuse an expensive result inside a comprehension filter.

Python
if (n := len("hello")) > 3:
    print(n)     # 5

Try it

Python Capture while you test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Parentheses and scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
name := expression
Part Description
name A plain identifier. Attributes (a.b), subscripts (a[0]) and unpacking targets are not allowed.
expression Any expression, evaluated first.

Return value: the value of expression, which is also bound to name.

Raises: SyntaxError for unparenthesized use at the top level of an expression statement, for invalid targets, and for rebinding a comprehension's own iteration variable.

Usage notes

  • Parenthesize in most places. := has the lowest precedence of all operators, so write if (n := f()) > 3, not if n := f() > 3 (that would assign the comparison result). It is allowed without parentheses only in a few positions, such as if x := f(): or while chunk := f.read(1024):, but adding parentheses never hurts.
  • Not allowed bare as a full statement (x := 1) or as an annotated assignment target; use = there.
  • Scope. In a normal block, the name is an ordinary local. Inside a comprehension or generator expression the name is bound in the containing function or module, so it stays visible afterwards, unlike the loop variable.
  • Cannot rebind the comprehension's iteration variable, and cannot be used in the iterable expression of a comprehension.
  • Short-circuiting applies. In a and (b := f()) the name b may remain unbound if a is falsy, so avoid using it afterwards without checking.
  • Good fits: regex matches, while loops that read data, filtering with an expensive function you also want to keep, and "compute once, test, then use".
  • Poor fits: anywhere a plain two-line assignment is clearer. Do not pile several walruses into one line.
  • Type hints. A type checker infers the variable's type from the assigned expression; you cannot annotate it in place.