Skip to content

Commit c074696

Browse files
miss-islingtonlul-casmas192johnslavik
authored
[3.14] gh-65058: Document the grouping of conditional expressions (GH-162167) (GH-162753)
gh-65058: Document the grouping of conditional expressions (GH-162167) 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. (cherry picked from commit 08784b2) Co-authored-by: Lucas <lucasefernandes333@gmail.com> Co-authored-by: Sam Joshva <samjoshva@gmail.com> Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent 308d20e commit c074696

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎Doc/reference/expressions.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,15 @@ The expression ``x if C else y`` first evaluates the condition, *C* rather than
21912191
If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
21922192
evaluated and its value is returned.
21932193

2194+
Conditional expressions group from right to left, so
2195+
``a if b else c if d else e`` means ``a if b else (c if d else e)``::
2196+
2197+
>>> n = 2
2198+
>>> print('positive' if n > 0
2199+
... else 'negative' if n < 0
2200+
... else 'zero')
2201+
positive
2202+
21942203
See :pep:`308` for more details about conditional expressions.
21952204

21962205

0 commit comments

Comments
 (0)