Repository navigation
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
Changes from 1 commit
57d7454
987acca
80e3f7d
ada2b88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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.* | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| One common use case is when handling the matched data in regular expression | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grammar:
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** | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. code-block:: python | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (matching := pattern.search(data)): | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for parentheses here:
Suggested change
|
||||||||||||||||||||||||||||
| do_something(matching) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Another popular use case is when processing the streams in a | ||||||||||||||||||||||||||||
| chunk from the file. | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| **Current scenario** | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. code-block:: python | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| chunk = file.read(9000) | ||||||||||||||||||||||||||||
| while chunk: | ||||||||||||||||||||||||||||
| process(chunk) | ||||||||||||||||||||||||||||
| chunk = file.read(9000) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| **Using assignment expression** | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We typically add
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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!