Skip to content
Merged
Changes from 1 commit
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
56 changes: 55 additions & 1 deletion Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't think this needs to include the history or motivation... just the behavior. We can reuse the above grammar for the explanation, too!

Suggested change
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**.
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`.

**Examples**

*The following examples demonstrate the use case of an assignment expressions.*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar:

Suggested change
One common use case is when handling the matched data in regular expression
One common use case is when handling matched regular expressions:


**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**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**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)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for parentheses here:

Suggested change
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused by the original wording... something like this is clearer to me:

Suggested change
Another popular use case is when processing the streams in a
chunk from the file.
Or, when processing a file stream in chunks:


**Current scenario**

.. code-block:: python

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

**Using assignment expression**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Current scenario**
.. code-block:: python
chunk = file.read(9000)
while chunk:
process(chunk)
chunk = file.read(9000)
**Using assignment expression**

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the commit.


.. code-block:: python

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


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically add versionadded directives for new features:

Suggested change
.. versionadded:: 3.8

Expand Down