From ba6ac10294695773b6c5313291e4b145628394b6 Mon Sep 17 00:00:00 2001 From: lul-cas Date: Tue, 17 Nov 2026 15:31:45 -0300 Subject: [PATCH 1/4] gh-65058: Note that conditional expressions are not evaluated left to right The "Evaluation order" section said that expressions are always evaluated from left to right, but a conditional expression evaluates its condition first. --- Doc/reference/expressions.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index dba85db67b65e27..56cff413872d3ec 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -2526,8 +2526,10 @@ 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 +:ref:`conditional expressions `, which evaluate the condition first). +Notice that while evaluating an assignment, the right-hand side is evaluated +before the left-hand side. In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:: @@ -2538,6 +2540,7 @@ their suffixes:: expr1 + expr2 * (expr3 - expr4) expr1(expr2, expr3, *expr4, **expr5) expr3, expr4 = expr1, expr2 + expr2 if expr1 else expr3 .. _operator-summary: From f7f8f560aaf28cbdb928b1ad0844af0b48e17257 Mon Sep 17 00:00:00 2001 From: lul-cas Date: Tue, 17 Nov 2026 15:31:45 -0300 Subject: [PATCH 2/4] gh-65058: Document the grouping of conditional expressions Add a sentence and an example showing that chained conditional expressions group from right to left. Co-authored-by: Sam Joshva --- Doc/reference/expressions.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 56cff413872d3ec..3b8ccdcd9e82d91 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -2423,6 +2423,14 @@ The expression ``x if C else y`` first evaluates the condition, *C* rather than If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is evaluated and its value is returned. +Conditional expressions group from right to left, so +``a if b else c if d else e`` means ``a if b else (c if d else e)``:: + + >>> 'foo' if 1 else 'bar' if 0 else 'baz' + 'foo' + >>> ('foo' if 1 else 'bar') if 0 else 'baz' + 'baz' + See :pep:`308` for more details about conditional expressions. From 25f6b49ea0730994cb5e1d4711d8d0a338de946f Mon Sep 17 00:00:00 2001 From: lul-cas Date: Tue, 17 Nov 2026 22:02:03 -0300 Subject: [PATCH 3/4] Revert "gh-65058: Note that conditional expressions are not evaluated left to right" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ba6ac10294695773b6c5313291e4b145628394b6. Co-authored-by: Bartosz Sławecki --- Doc/reference/expressions.rst | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 3b8ccdcd9e82d91..3de013a4290613b 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -2534,10 +2534,8 @@ Evaluation order .. index:: pair: evaluation; order -Python evaluates expressions from left to right (except for -:ref:`conditional expressions `, which evaluate the condition first). -Notice that while evaluating an assignment, the right-hand side is evaluated -before the left-hand side. +Python evaluates expressions from left to right. Notice that while evaluating +an assignment, the right-hand side is evaluated before the left-hand side. In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:: @@ -2548,7 +2546,6 @@ their suffixes:: expr1 + expr2 * (expr3 - expr4) expr1(expr2, expr3, *expr4, **expr5) expr3, expr4 = expr1, expr2 - expr2 if expr1 else expr3 .. _operator-summary: From c6f180c65b582337e40d6fc2fbe62f14ae29ce0a Mon Sep 17 00:00:00 2001 From: lul-cas Date: Tue, 17 Nov 2026 22:03:51 -0300 Subject: [PATCH 4/4] gh-65058: Use True/False and spam/eggs/ham in the grouping example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartosz Sławecki --- Doc/reference/expressions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 3de013a4290613b..6c4ede769805097 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -2426,10 +2426,10 @@ evaluated and its value is returned. Conditional expressions group from right to left, so ``a if b else c if d else e`` means ``a if b else (c if d else e)``:: - >>> 'foo' if 1 else 'bar' if 0 else 'baz' - 'foo' - >>> ('foo' if 1 else 'bar') if 0 else 'baz' - 'baz' + >>> 'spam' if True else 'eggs' if False else 'ham' + 'spam' + >>> ('spam' if True else 'eggs') if False else 'ham' + 'ham' See :pep:`308` for more details about conditional expressions.