Skip to content

Commit 755cb49

Browse files
bpo-39868: Add documentation for Assignment Expressions (walrus, PEP 572) (GH-18851)
(cherry picked from commit f117cef) Co-authored-by: Shankar Jha <[email protected]>
1 parent b99f770 commit 755cb49

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Doc/reference/expressions.rst

+19-2
Original file line numberDiff line numberDiff line change
@@ -1650,9 +1650,26 @@ Assignment expressions
16501650
.. productionlist::
16511651
assignment_expression: [`identifier` ":="] `expression`
16521652

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

1655-
See :pep:`572` for more details about assignment expressions.
1657+
One common use case is when handling matched regular expressions:
1658+
1659+
.. code-block:: python
1660+
1661+
if matching := pattern.search(data):
1662+
do_something(matching)
1663+
1664+
Or, when processing a file stream in chunks:
1665+
1666+
.. code-block:: python
1667+
1668+
while chunk := file.read(9000):
1669+
process(chunk)
1670+
1671+
.. versionadded:: 3.8
1672+
See :pep:`572` for more details about assignment expressions.
16561673

16571674

16581675
.. _if_expr:

0 commit comments

Comments
 (0)