Skip to content

gh-65058: Document the grouping of conditional expressions - #162167

Open
lul-cas wants to merge 4 commits into
python:mainfrom
lul-cas:fix-issue-65058
Open

lul-cas wants to merge 4 commits into
python:mainfrom
lul-cas:fix-issue-65058

Conversation

@lul-cas

@lul-cas lul-cas commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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 :)

lul-cas and others added 2 commits November 17, 2026 15:31
…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>
@read-the-docs-community

read-the-docs-community Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35971675 | 📁 Comparing c6f180c against main (c7fef83)

  🔍 Preview build  

3 files changed
± library/os.html
± reference/expressions.html
± whatsnew/changelog.html

Comment thread Doc/reference/expressions.rst Resolved
Comment thread Doc/reference/expressions.rst Resolved
Comment thread Doc/reference/expressions.rst Resolved
@lul-cas lul-cas changed the title gh-65058: Document evaluation order and grouping of conditional expressions gh-65058: Document the grouping of conditional expressions Nov 18, 2026
lul-cas and others added 2 commits November 17, 2026 22:07
…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>
@lul-cas
lul-cas requested a review from johnslavik November 18, 2026 01:14
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'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants