Skip to content

Commit f7f8f56

Browse files
lul-casmas192
andcommitted
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 <samjoshva@gmail.com>
1 parent ba6ac10 commit f7f8f56

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎Doc/reference/expressions.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,14 @@ The expression ``x if C else y`` first evaluates the condition, *C* rather than
24232423
If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
24242424
evaluated and its value is returned.
24252425

2426+
Conditional expressions group from right to left, so
2427+
``a if b else c if d else e`` means ``a if b else (c if d else e)``::
2428+
2429+
>>> 'foo' if 1 else 'bar' if 0 else 'baz'
2430+
'foo'
2431+
>>> ('foo' if 1 else 'bar') if 0 else 'baz'
2432+
'baz'
2433+
24262434
See :pep:`308` for more details about conditional expressions.
24272435

24282436

0 commit comments

Comments
 (0)