Repository navigation
Conversation
…eft to right The "Evaluation order" section said that expressions are always evaluated from left to right, but a conditional expression evaluates its condition first.
Add a sentence and an example showing that chained conditional expressions group from right to left. Co-authored-by: Sam Joshva <samjoshva@gmail.com>
Documentation build overview
|
johnslavik
requested changes
Nov 18, 2026
…luated left to right" This reverts commit ba6ac10. Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
encukou
reviewed
Nov 18, 2026
Comment on lines
+2429
to
+2432
| >>> 'spam' if True else 'eggs' if False else 'ham' | ||
| 'spam' | ||
| >>> ('spam' if True else 'eggs') if False else 'ham' | ||
| 'ham' |
Member
There was a problem hiding this comment.
Sorry to spoil the fun, but I don't think this example adds too much clarity: it feels like someone who doesn't already know what they're looking for would have trouble parsing it.
I'd prefer if the result had more of a connection to the conditions -- something like:
Suggested change
| >>> 'spam' if True else 'eggs' if False else 'ham' | |
| 'spam' | |
| >>> ('spam' if True else 'eggs') if False else 'ham' | |
| 'ham' | |
| >>> n = 2 | |
| >>> 'positive' if n > 0 else 'negative' if n < 0 else 'zero' | |
| 'positive' |
The switched-around version for such an example would probably be best omitted, though:
>>> ('bizarre' if n > 0 else 'negative') if n < 0 else 'non-negative'
'non-negative'What do you think about using newlines to make the grouping in such a “ladder” clearer, like this?
>>> n = 2
>>> print('positive' if n > 0
... else 'negative' if n < 0
... else 'zero')
positive(Personally I wish I'd seen this style sooner than I did; it helped me wrap my head around the priority.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This continues #119171 by @mas192, who agreed to let me take it over, and supersedes the original PR
Two of the three points raised in the issue have already been addressed on main:
This PR addresses the remaining grouping point:
Conditional expressions: Clarifies that they group from right to left, using an example based on the one from #119171.
The evaluation order change was dropped after review.
Co-authored-by: Sam Joshva samjoshva@gmail.com :)