PEP 572

E51179

PEP 572 is the Python proposal that introduced the “walrus operator” (:=) for assignment expressions, allowing assignment within larger expressions.

Jump to: Statements Referenced by

Statements (48)

Predicate Object
instanceOf Python Enhancement Proposal
affectsComponent Python compiler
Python language syntax
author Chris Angelico
bdfDelegate Guido van Rossum
category Python syntax change
controversy contributed to Guido van Rossum stepping down as BDFL
sparked debate in the Python community about readability
created 2018-02-25
decisionProcess BDFL-delegate model
definesSyntax NAME ":=" expression
designGoal avoid ambiguity with existing assignment syntax
keep assignment expressions visually distinct from equality tests
disallowsContext assignment expressions at top level of comprehension target list
assignment expressions in some lambda parameter positions
discussedOn python-dev mailing list
python-ideas mailing list
examplePattern [y := f(x) for x in data if y > 0]
if (m := pattern.search(data)) is not None: ...
while (line := file.readline()) != "": ...
firstAppearedInVersion Python 3.8
governingBody Python core developers
introducedFeature assignment expressions
introducedOperator walrus operator
introducedSymbol :=
language Python
motivation allow assignment within larger expressions
improve readability for some conditional and comprehension constructs
reduce code duplication in certain patterns
number 572
pepUrl https://peps.python.org/pep-0572/
referenceImplementation Python reference implementation (CPython)
surface form: CPython 3.8
relatedConcept assignment
comprehensions
control flow
expressions
relatedPEP PEP 8
repository https://github.com/python/peps
requiresVersionAtLeast Python 3.8
sponsor Guido van Rossum
status Final
syntaxChoiceReason use of := inspired by languages like Go and Pascal
title Assignment Expressions
type Standards Track
useCase if statements that test and capture a value
list comprehensions with reused subexpressions
regular expression matches stored in a variable
while loops that both test and assign

Referenced by (2)

Full triples — surface form annotated when it differs from this entity's canonical label.

PEP 622 relatedTo PEP 572