Skip to content

[3.9] bpo-39868: Add documentation for Assignment Expressions (walrus, PEP 572) (GH-18851) #21622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1650,9 +1650,26 @@ Assignment expressions
.. productionlist::
assignment_expression: [`identifier` ":="] `expression`

.. TODO: BPO-39868
An assignment expression (sometimes also called a "named expression" or
"walrus") assigns an :token:`expression` to an :token:`identifier`, while also
returning the value of the :token:`expression`.

See :pep:`572` for more details about assignment expressions.
One common use case is when handling matched regular expressions:

.. code-block:: python

if matching := pattern.search(data):
do_something(matching)

Or, when processing a file stream in chunks:

.. code-block:: python

while chunk := file.read(9000):
process(chunk)

.. versionadded:: 3.8
See :pep:`572` for more details about assignment expressions.


.. _if_expr:
Expand Down