Skip to content

bpo-39868: Add the documentation for Assignment Expressions (PEP 572). #18851

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 4 commits into from
Jul 25, 2020

Conversation

shankarj67
Copy link
Contributor

@shankarj67 shankarj67 commented Mar 8, 2020

@brandtbucher
Copy link
Member

brandtbucher commented Mar 8, 2020

Thanks for your time @shankarj67, and welcome to CPython! 😎

I'll review this in a bit. Adding "skip news" since the one for #18793 is fine here as well.

Also, can you change the PR title to reference "bpo-39868" instead of "bpo-39702"?

@shankarj67 shankarj67 changed the title bpo-39702: Add the documentation for Assignment Expressions (PEP 572). bpo-39868: Add the documentation for Assignment Expressions (PEP 572). Mar 8, 2020
Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

This is good, thanks!

It's a bit longer than I think it needs to be, though (see similar sections, which are quite brief and have no subheadings). I also don't think we need examples of code without assignment expressions, since we're not trying to sell the syntax (like the PEP), just explaining it!

Before we get to grammar, etc., I've identified three chunks that can probably be removed, for the reasons above.

Comment on lines 1661 to 1665

**Examples**

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

Copy link
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.*

Comment on lines 1668 to 1680

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

Comment on lines 1690 to 1700

**Current scenario**

.. code-block:: python

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

**Using assignment expression**
Copy link
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
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.

Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

I've added some suggestions (mostly grammar and style):


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


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

Copy link
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

Comment on lines 1670 to 1671
Another popular use case is when processing the streams in a
chunk from the file.
Copy link
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:


.. code-block:: python

if (matching := pattern.search(data)):
Copy link
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):

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
Copy link
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:

Comment on lines 1652 to 1661
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
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`.

@shankarj67
Copy link
Contributor Author

Thank you for the suggestion. I have added the new commit.


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

Choose a reason for hiding this comment

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

Thanks! One more tiny fix:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@brandtbucher
Copy link
Member

Closing and reopening to trigger CI.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

This looks fine to me. Is it waiting for anything?

@brandtbucher
Copy link
Member

Just a merge! ;)

@gvanrossum gvanrossum merged commit f117cef into python:master Jul 25, 2020
@bedevere-bot

This comment has been minimized.

@miss-islington
Copy link
Contributor

Thanks @shankarj67 for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 25, 2020
@bedevere-bot
Copy link

GH-21622 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Jul 25, 2020
@bedevere-bot
Copy link

GH-21623 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 25, 2020
miss-islington added a commit that referenced this pull request Jul 25, 2020
…572) (GH-18851)

(cherry picked from commit f117cef)

Co-authored-by: Shankar Jha <[email protected]>
miss-islington added a commit that referenced this pull request Jul 25, 2020
…572) (GH-18851)

(cherry picked from commit f117cef)

Co-authored-by: Shankar Jha <[email protected]>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Aug 4, 2020
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Aug 20, 2020
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants