diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-07-25 23:40:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-25 23:40:48 (GMT) |
commit | 616734b6c80e222f16249e9a9ce52588a0b611a7 (patch) | |
tree | 328337193643a3b0a95c721d87ac11de4dda8e72 /Doc | |
parent | fdc91c2e6096f60ddb8b7f72fe7926ccbbf42800 (diff) | |
download | cpython-616734b6c80e222f16249e9a9ce52588a0b611a7.zip cpython-616734b6c80e222f16249e9a9ce52588a0b611a7.tar.gz cpython-616734b6c80e222f16249e9a9ce52588a0b611a7.tar.bz2 |
bpo-39868: Add documentation for Assignment Expressions (walrus, PEP 572) (GH-18851)
(cherry picked from commit f117cef25b5ffc4db9fbe373ddb65e14f59f0397)
Co-authored-by: Shankar Jha <shankarj67@gmail.com>
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/expressions.rst | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 8036a49..18abce3 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -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: |