From 57d7454615e0d8dbcc535e262fa26dcd426f85f2 Mon Sep 17 00:00:00 2001 From: shankarj67 Date: Sun, 8 Mar 2020 18:46:32 +0530 Subject: [PATCH 1/4] bpo-39702: Add the documentation for Assignment Expressions (PEP 572). --- Doc/reference/expressions.rst | 56 ++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 3fcc5e17d9a7cd..bc02706df57c34 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1649,7 +1649,61 @@ Assignment expressions .. productionlist:: assignment_expression: [`identifier` ":="] `expression` -.. TODO: BPO-39868 +Assignment expression is the new expression introduced in the Python3.8 +or later (sometimes also called as "named expression" or "walrus operator"), +which aims to reduce the number of lines of code by allowing the +assignment and return of value in the same expression. This makes codebase +more readable and maintainable. + +The expression ``name := expr`` where **expr** is any valid Python expression +other than an unparenthesize tuple and the **name** is an identifier. +Here the variable **name** holds the assigned value of a given **expression**. + +**Examples** + +*The following examples demonstrate the use case of an assignment expressions.* + + +One common use case is when handling the matched data in regular expression + +**Current scenario** + +.. code-block:: python + + matching = patern.search(data) + if matching: + do_something(matching) + +Notice that a matching variable is being called two times in the code +that may be replaced using assignment expression in one line. + +**Using assignment expression** + +.. code-block:: python + + if (matching := pattern.search(data)): + do_something(matching) + + +Another popular use case is when processing the streams in a +chunk from the file. + +**Current scenario** + +.. code-block:: python + + chunk = file.read(9000) + while chunk: + process(chunk) + chunk = file.read(9000) + +**Using assignment expression** + +.. code-block:: python + + while chunk := file.read(9000): + process(chunk) + See :pep:`572` for more details about assignment expressions. From 987acca49c9c4dfc7e4cec04de94d78f0e2b9bb4 Mon Sep 17 00:00:00 2001 From: shankarj67 Date: Mon, 9 Mar 2020 00:08:23 +0530 Subject: [PATCH 2/4] bpo-39868: Update the content. --- Doc/reference/expressions.rst | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index bc02706df57c34..8329d038d143d1 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1659,46 +1659,17 @@ The expression ``name := expr`` where **expr** is any valid Python expression other than an unparenthesize tuple and the **name** is an identifier. Here the variable **name** holds the assigned value of a given **expression**. -**Examples** - -*The following examples demonstrate the use case of an assignment expressions.* - One common use case is when handling the matched data in regular expression -**Current scenario** - -.. code-block:: python - - matching = patern.search(data) - if matching: - do_something(matching) - -Notice that a matching variable is being called two times in the code -that may be replaced using assignment expression in one line. - -**Using assignment expression** - .. code-block:: python if (matching := pattern.search(data)): do_something(matching) - Another popular use case is when processing the streams in a chunk from the file. -**Current scenario** - -.. code-block:: python - - chunk = file.read(9000) - while chunk: - process(chunk) - chunk = file.read(9000) - -**Using assignment expression** - .. code-block:: python while chunk := file.read(9000): From 80e3f7d87601153cd00b6ef23510813eec6b3456 Mon Sep 17 00:00:00 2001 From: shankarj67 Date: Tue, 10 Mar 2020 22:31:32 +0530 Subject: [PATCH 3/4] bpo-39868: Update the style and grammar. --- Doc/reference/expressions.rst | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 8329d038d143d1..7957200eeed37c 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1649,34 +1649,26 @@ Assignment expressions .. productionlist:: assignment_expression: [`identifier` ":="] `expression` -Assignment expression is the new expression introduced in the Python3.8 -or later (sometimes also called as "named expression" or "walrus operator"), -which aims to reduce the number of lines of code by allowing the -assignment and return of value in the same expression. This makes codebase -more readable and maintainable. +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`. -The expression ``name := expr`` where **expr** is any valid Python expression -other than an unparenthesize tuple and the **name** is an identifier. -Here the variable **name** holds the assigned value of a given **expression**. - - -One common use case is when handling the matched data in regular expression +One common use case is when handling matched regular expression: .. code-block:: python - if (matching := pattern.search(data)): + if matching := pattern.search(data): do_something(matching) -Another popular use case is when processing the streams in a -chunk from the file. +Or, when processing a file stream in chunks: .. code-block:: python while chunk := file.read(9000): process(chunk) - -See :pep:`572` for more details about assignment expressions. +.. versionadded:: 3.8 + See :pep:`572` for more details about assignment expressions. .. _if_expr: From ada2b880ea9d8eecd251cf8a40e7489f0be5813d Mon Sep 17 00:00:00 2001 From: shankarj67 Date: Tue, 10 Mar 2020 23:14:29 +0530 Subject: [PATCH 4/4] bpo-39868: Update the spelling. --- Doc/reference/expressions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 7957200eeed37c..2a273c9813df38 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1653,7 +1653,7 @@ 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`. -One common use case is when handling matched regular expression: +One common use case is when handling matched regular expressions: .. code-block:: python