|
29 | 29 | %====================================================================== |
30 | 30 | \section{PEP 308: Conditional Expressions} |
31 | 31 |
|
32 | | -% XXX write this |
| 32 | +For a long time, people have been requesting a way to write |
| 33 | +conditional expressions, expressions that return value A or value B |
| 34 | +depending on whether a Boolean value is true or false. A conditional |
| 35 | +expression lets you write a single assignment statement that has the |
| 36 | +same effect as the following: |
| 37 | + |
| 38 | +\begin{verbatim} |
| 39 | +if condition: |
| 40 | + x = true_value |
| 41 | +else: |
| 42 | + x = false_value |
| 43 | +\end{verbatim} |
| 44 | + |
| 45 | +There have been endless tedious discussions of syntax on both |
| 46 | +python-dev and comp.lang.python, and even a vote that found the |
| 47 | +majority of voters wanted some way to write conditional expressions, |
| 48 | +but there was no syntax that was clearly preferred by a majority. |
| 49 | +Candidates include C's \code{cond ? true_v : false_v}, |
| 50 | +\code{if cond then true_v else false_v}, and 16 other variations. |
| 51 | + |
| 52 | +GvR eventually chose a surprising syntax: |
| 53 | + |
| 54 | +\begin{verbatim} |
| 55 | +x = true_value if condition else false_value |
| 56 | +\end{verbatim} |
| 57 | + |
| 58 | +Evaluation is still lazy as in existing Boolean expression, so the |
| 59 | +evaluation jumps around a bit. The \var{condition} expression is |
| 60 | +evaluated first, and the \var{true_value} expression is evaluated only |
| 61 | +if the condition was true. Similarly, the \var{false_value} |
| 62 | +expression is only evaluated when the condition is false. |
| 63 | + |
| 64 | +This syntax may seem strange and backwards; why does the condition go |
| 65 | +in the \emph{middle} of the expression, and not in the front as in C's |
| 66 | +\code{c ? x : y}? The decision was checked by applying the new syntax |
| 67 | +to the modules in the standard library and seeing how the resulting |
| 68 | +code read. In many cases where a conditional expression is used, one |
| 69 | +value seems to be the 'common case' and one value is an 'exceptional |
| 70 | +case', used only on rarer occasions when the condition isn't met. The |
| 71 | +conditional syntax makes this pattern a bit more obvious: |
| 72 | + |
| 73 | +\begin{verbatim} |
| 74 | +contents = ((doc + '\n') if doc else '') |
| 75 | +\end{verbatim} |
| 76 | + |
| 77 | +I read the above statement as meaning ``here \var{contents} is |
| 78 | +usually assigned a value of \code{doc+'\n'}; sometimes |
| 79 | +\var{doc} is empty, in which special case an empty string is returned.'' |
| 80 | +I doubt I will use conditional expressions very often where there |
| 81 | +isn't a clear common and uncommon case. |
| 82 | + |
| 83 | +There was some discussion of whether the language should require |
| 84 | +surrounding conditional expressions with parentheses. The decision |
| 85 | +was made to \emph{not} require parentheses in the Python language's |
| 86 | +grammar, but as a matter of style I think you should always use them. |
| 87 | +Consider these two statements: |
| 88 | + |
| 89 | +\begin{verbatim} |
| 90 | +# First version -- no parens |
| 91 | +level = 1 if logging else 0 |
| 92 | +
|
| 93 | +# Second version -- with parens |
| 94 | +level = (1 if logging else 0) |
| 95 | +\end{verbatim} |
| 96 | + |
| 97 | +In the first version, I think a reader's eye might group the statement |
| 98 | +into 'level = 1', 'if logging', 'else 0', and think that the condition |
| 99 | +decides whether the assignment to \var{level} is performed. The |
| 100 | +second version reads better, in my opinion, because it makes it clear |
| 101 | +that the assignment is always performed and the choice is being made |
| 102 | +between two values. |
| 103 | + |
| 104 | +Another reason for including the brackets: a few odd combinations of |
| 105 | +list comprehensions and lambdas could look like incorrect conditional |
| 106 | +expressions. See \pep{308} for some examples. If you put parentheses |
| 107 | +around your conditional expressions, you won't run into this case. |
| 108 | + |
| 109 | + |
| 110 | +\begin{seealso} |
| 111 | + |
| 112 | +\seepep{308}{Conditional Expressions}{PEP written by |
| 113 | +Guido van Rossum and Raymond D. Hettinger; implemented by Thomas |
| 114 | +Wouters.} |
| 115 | + |
| 116 | +\end{seealso} |
33 | 117 |
|
34 | 118 |
|
35 | 119 | %====================================================================== |
|
0 commit comments