Repository navigation
gh-65058 Revised conditional expression with additional context and examples for issue #119171
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 all commits
9298051
a285684
8beb707
1a98e9e
a461ca6
137c275
9341d0f
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 |
|---|---|---|
|
|
@@ -1905,18 +1905,35 @@ Evaluation order | |
|
|
||
| .. index:: pair: evaluation; order | ||
|
|
||
| Python evaluates expressions from left to right. Notice that while evaluating | ||
| an assignment, the right-hand side is evaluated before the left-hand side. | ||
| Python evaluates expressions from left to right (except for conditional_expression). | ||
| Notice that while evaluating an assignment, the right-hand side is evaluated before | ||
| the left-hand side. | ||
|
|
||
| Examples for left to right: | ||
|
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. Perhaps “Example [singular] of left-to-right grouping”? |
||
|
|
||
| .. code-block:: python | ||
|
|
||
| 2 + 3 * 4 / 2 - 1 | ||
| 7 | ||
|
|
||
| Following is an example of right to left conditional_expression evaluation example: | ||
|
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. Example of right-to-left conditional expression grouping |
||
|
|
||
| .. code-block:: python | ||
|
|
||
| 'foo' if 1 else 'bar' if 0 else 'baz' | ||
| 'foo' | ||
|
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 wonder if these would be clearer if they included the interactive prompt >>> |
||
|
|
||
| In the following lines, expressions will be evaluated in the arithmetic order of | ||
| their suffixes:: | ||
| their suffixes: | ||
|
|
||
| .. code-block:: python | ||
|
Contributor
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. You can just keep the
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. A explicit code block has better emphasis compared to to the :
Contributor
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. Only 18 files use
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. Thank you for pointing out to the inconsistencies. Since, .. code-block:: python
Contributor
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. ... just use
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. The original pr I made was for adding examples and clarity to the documentation. I will wait for a member to review and approve or comment on that before I make any more changes. Thanks for the feedback
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. Personally, I prefer There is a difference between Hello::
...equivalent to Hello:
.. code-block::
...See https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#literal-blocks. |
||
|
|
||
| expr1, expr2, expr3, expr4 | ||
| (expr1, expr2, expr3, expr4) | ||
| {expr1: expr2, expr3: expr4} | ||
| expr1 + expr2 * (expr3 - expr4) | ||
| expr1(expr2, expr3, *expr4, **expr5) | ||
| expr3, expr4 = expr1, expr2 | ||
| expr1, expr2, expr3, expr4 | ||
| (expr1, expr2, expr3, expr4) | ||
| {expr1: expr2, expr3: expr4} | ||
| expr1 + expr2 * (expr3 - expr4) | ||
| expr1(expr2, expr3, *expr4, **expr5) | ||
| expr3, expr4 = expr1, expr2 | ||
|
|
||
|
|
||
| .. _operator-summary: | ||
|
|
@@ -1930,8 +1947,11 @@ Operator precedence | |
| The following table summarizes the operator precedence in Python, from highest | ||
| precedence (most binding) to lowest precedence (least binding). Operators in | ||
| the same box have the same precedence. Unless the syntax is explicitly given, | ||
| operators are binary. Operators in the same box group left to right (except for | ||
| exponentiation and conditional expressions, which group from right to left). | ||
| operators are binary. Operators in the same box group left to right (except for | ||
| comparisons, including tests, exponentiation and conditional expressions, which | ||
| group from right to left which all have the same precedence and chain from left | ||
| to right — see section Comparisons — and exponentiation, which groups from right | ||
| to left) | ||
|
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. Exponentiation listed twice. The exceptions don’t all have the same precedence. Partially redundant with the paragraph below. |
||
|
|
||
| Note that comparisons, membership tests, and identity tests, all have the same | ||
| precedence and have a left-to-right chaining feature as described in the | ||
|
|
||
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.
Everywhere else the existing text just refers to them as conditional expressions in plural and without the underscore.
I’m not sure pointing out conditional expressions as the only exception is much better. Perhaps it should be except for conditional and exponentiation expressions?.
I think this sentence confuses the usage of evaluates. Perhaps we actually mean groups expressions? Compare the use of evaluate in the next sentence, meaning finding the value of operands. But then we’d be just repeating the “Operators . . . group” sentence in from the next section. Otherwise, see Terry’s 2nd point in #65058, about evaluating the middle first.
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.
Why do you think so?
Uh oh!
There was an error while loading. Please reload this page.
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.
Ah sorry I was confused. Perhaps the “left to right except conditional” got me thinking “conditional expressions must be the opposite, right to left” and therefore we must be talking about grouping, not evaluation.