-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Conversation
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"? |
There was a problem hiding this 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.
Doc/reference/expressions.rst
Outdated
|
||
**Examples** | ||
|
||
*The following examples demonstrate the use case of an assignment expressions.* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**Examples** | |
*The following examples demonstrate the use case of an assignment expressions.* |
Doc/reference/expressions.rst
Outdated
|
||
**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** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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** |
Doc/reference/expressions.rst
Outdated
|
||
**Current scenario** | ||
|
||
.. code-block:: python | ||
|
||
chunk = file.read(9000) | ||
while chunk: | ||
process(chunk) | ||
chunk = file.read(9000) | ||
|
||
**Using assignment expression** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**Current scenario** | |
.. code-block:: python | |
chunk = file.read(9000) | |
while chunk: | |
process(chunk) | |
chunk = file.read(9000) | |
**Using assignment expression** |
There was a problem hiding this comment.
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.
There was a problem hiding this 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. | ||
|
There was a problem hiding this comment.
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:
.. versionadded:: 3.8 | |
Doc/reference/expressions.rst
Outdated
Another popular use case is when processing the streams in a | ||
chunk from the file. |
There was a problem hiding this comment.
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:
Another popular use case is when processing the streams in a | |
chunk from the file. | |
Or, when processing a file stream in chunks: |
Doc/reference/expressions.rst
Outdated
|
||
.. code-block:: python | ||
|
||
if (matching := pattern.search(data)): |
There was a problem hiding this comment.
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:
if (matching := pattern.search(data)): | |
if matching := pattern.search(data): |
Doc/reference/expressions.rst
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar:
One common use case is when handling the matched data in regular expression | |
One common use case is when handling matched regular expressions: |
Doc/reference/expressions.rst
Outdated
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**. | ||
|
There was a problem hiding this comment.
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!
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`. |
Thank you for the suggestion. I have added the new commit. |
Doc/reference/expressions.rst
Outdated
|
||
See :pep:`572` for more details about assignment expressions. | ||
One common use case is when handling matched regular expression: |
There was a problem hiding this comment.
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:
One common use case is when handling matched regular expression: | |
One common use case is when handling matched regular expressions: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Closing and reopening to trigger CI. |
There was a problem hiding this 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?
Just a merge! ;) |
This comment has been minimized.
This comment has been minimized.
Thanks @shankarj67 for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9. |
…572) (pythonGH-18851) (cherry picked from commit f117cef) Co-authored-by: Shankar Jha <[email protected]>
GH-21622 is a backport of this pull request to the 3.9 branch. |
GH-21623 is a backport of this pull request to the 3.8 branch. |
…572) (pythonGH-18851) (cherry picked from commit f117cef) Co-authored-by: Shankar Jha <[email protected]>
…572) (GH-18851) (cherry picked from commit f117cef) Co-authored-by: Shankar Jha <[email protected]>
…572) (GH-18851) (cherry picked from commit f117cef) Co-authored-by: Shankar Jha <[email protected]>
https://bugs.python.org/issue39868
https://bugs.python.org/issue39702