2003-02-07 12:03:31 -05:00
|
|
|
|
PEP: 308
|
|
|
|
|
Title: If-then-else expression
|
|
|
|
|
Version: $Revision$
|
|
|
|
|
Last-Modified: $Date$
|
2003-02-13 10:01:53 -05:00
|
|
|
|
Author: Guido van Rossum, Raymond D. Hettinger
|
2003-02-09 00:12:54 -05:00
|
|
|
|
Status: Draft
|
2003-02-07 12:03:31 -05:00
|
|
|
|
Type: Standards Track
|
|
|
|
|
Content-Type: text/plain
|
|
|
|
|
Created: 7-Feb-2003
|
2003-02-11 09:59:18 -05:00
|
|
|
|
Post-History: 7-Feb-2003, 11-Feb-2003
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
|
|
|
|
|
|
Requests for an if-then-else ("ternary") expression keep coming up
|
|
|
|
|
on comp.lang.python. This PEP contains a concrete proposal of a
|
|
|
|
|
fairly Pythonic syntax. This is the community's one chance: if
|
|
|
|
|
this PEP is approved with a clear majority, it will be implemented
|
|
|
|
|
in Python 2.4. If not, the PEP will be augmented with a summary
|
|
|
|
|
of the reasons for rejection and the subject better not come up
|
2003-02-13 10:01:53 -05:00
|
|
|
|
again. While the BDFL is co-author of this PEP, he is neither in
|
|
|
|
|
favor nor against this proposal; it is up to the community to
|
|
|
|
|
decide. If the community can't decide, the BDFL will reject the
|
|
|
|
|
PEP.
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
2003-02-11 10:53:20 -05:00
|
|
|
|
After unprecedented community response (very good arguments were
|
2003-02-11 09:59:18 -05:00
|
|
|
|
made both pro and con) this PEP has been revised with the help of
|
|
|
|
|
Raymond Hettinger. Without going through a complete revision
|
|
|
|
|
history, the main changes are a different proposed syntax, an
|
|
|
|
|
overview of proposed alternatives, the state of the curent
|
|
|
|
|
discussion, and a discussion of short-circuit behavior.
|
|
|
|
|
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
|
|
|
|
Proposal
|
|
|
|
|
|
|
|
|
|
The proposed syntax is as follows:
|
|
|
|
|
|
2003-02-11 00:43:56 -05:00
|
|
|
|
(if <condition>: <expression1> else: <expression2>)
|
|
|
|
|
|
2003-02-11 20:38:25 -05:00
|
|
|
|
Note that the enclosing parentheses are not optional.
|
2003-02-11 14:48:20 -05:00
|
|
|
|
|
|
|
|
|
The resulting expression is evaluated like this:
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
|
|
|
|
- First, <condition> is evaluated.
|
|
|
|
|
|
|
|
|
|
- If <condition> is true, <expression1> is evaluated and is the
|
|
|
|
|
result of the whole thing.
|
|
|
|
|
|
|
|
|
|
- If <condition> is false, <expression2> is evaluated and is the
|
|
|
|
|
result of the whole thing.
|
|
|
|
|
|
2003-02-11 17:34:52 -05:00
|
|
|
|
A natural extension of this syntax is to allow one or more 'elif'
|
|
|
|
|
parts:
|
|
|
|
|
|
|
|
|
|
(if <cond1>: <expr1> elif <cond2>: <expr2> ... else: <exprN>)
|
|
|
|
|
|
|
|
|
|
This will be implemented if the proposal is accepted.
|
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
The downsides to the proposal are:
|
|
|
|
|
|
|
|
|
|
* the required parentheses
|
|
|
|
|
* confusability with statement syntax
|
|
|
|
|
* additional semantic loading of colons
|
|
|
|
|
|
2003-02-07 12:03:31 -05:00
|
|
|
|
Note that at most one of <expression1> and <expression2> is
|
2003-02-11 09:59:18 -05:00
|
|
|
|
evaluated. This is called a "short-circuit expression"; it is
|
|
|
|
|
similar to the way the second operand of 'and' / 'or' is only
|
|
|
|
|
evaluated if the first operand is true / false.
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
2003-02-11 09:59:18 -05:00
|
|
|
|
A common way to emulate an if-then-else expression is:
|
2003-02-07 19:56:13 -05:00
|
|
|
|
|
|
|
|
|
<condition> and <expression1> or <expression2>
|
|
|
|
|
|
|
|
|
|
However, this doesn't work the same way: it returns <expression2>
|
|
|
|
|
when <expression1> is false! See FAQ 4.16 for alternatives that
|
2003-02-11 09:59:18 -05:00
|
|
|
|
work -- however, they are pretty ugly and require much more effort
|
|
|
|
|
to understand.
|
2003-02-07 19:56:13 -05:00
|
|
|
|
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
|
|
|
|
Alternatives
|
|
|
|
|
|
2003-02-11 20:38:25 -05:00
|
|
|
|
Holger Krekel proposed a new, minimally invasive variant:
|
|
|
|
|
|
2003-02-12 07:35:59 -05:00
|
|
|
|
<condition> and <expression1> else <expression2>
|
2003-02-11 20:38:25 -05:00
|
|
|
|
|
|
|
|
|
The concept behind it is that a nearly complete ternary operator
|
|
|
|
|
already exists with and/or and this proposal is the least invasive
|
|
|
|
|
change that makes it complete. Many respondants on the
|
|
|
|
|
newsgroup found this to be the most pleasing alternative.
|
|
|
|
|
However, a couple of respondants were able to post examples
|
2003-02-13 01:59:58 -05:00
|
|
|
|
that were mentally difficult to parse. Later it was pointed
|
|
|
|
|
out that this construct works by having the "else" change the
|
|
|
|
|
existing meaning of "and".
|
2003-02-11 20:38:25 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
As a result, there is increasing support for Christian Tismer's
|
|
|
|
|
proposed variant of the same idea:
|
2003-02-11 23:09:14 -05:00
|
|
|
|
|
2003-02-12 07:35:59 -05:00
|
|
|
|
<condition> then <expression1> else <expression2>
|
2003-02-11 23:09:14 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
The advantages are simple visual parsing, no required parenthesis,
|
|
|
|
|
no change in the semantics of existing keywords, not as likely
|
|
|
|
|
as the proposal to be confused with statement syntax, and does
|
|
|
|
|
not further overload the colon. The disadvantage is the
|
|
|
|
|
implementation costs of introducing a new keyword. However,
|
|
|
|
|
unlike other new keywords, the word "then" seems unlikely to
|
|
|
|
|
have been used as a name in existing programs.
|
|
|
|
|
|
2003-02-11 20:38:25 -05:00
|
|
|
|
---
|
|
|
|
|
|
2003-02-07 12:03:31 -05:00
|
|
|
|
Many C-derived languages use this syntax:
|
|
|
|
|
|
|
|
|
|
<condition> ? <expression1> : <expression2>
|
|
|
|
|
|
2003-02-11 00:43:56 -05:00
|
|
|
|
Eric Raymond even implemented this. The BDFL rejected this for
|
|
|
|
|
several reasons: the colon already has many uses in Python (even
|
2003-02-11 09:59:18 -05:00
|
|
|
|
though it would actually not be ambiguous, because the question
|
|
|
|
|
mark requires a matching colon); for people not used to C-derived
|
2003-02-07 19:56:13 -05:00
|
|
|
|
language, it is hard to understand.
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
2003-02-07 17:34:54 -05:00
|
|
|
|
---
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
The original version of this PEP proposed the following syntax:
|
2003-02-07 17:34:54 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
<expression1> if <condition> else <expression2>
|
2003-02-07 17:34:54 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
The out-of-order arrangement was found to be too uncomfortable
|
|
|
|
|
for many of participants in the discussion; especially when
|
|
|
|
|
<expression1> is long, it's easy to miss the conditional while
|
|
|
|
|
skimming.
|
2003-02-07 17:34:54 -05:00
|
|
|
|
|
|
|
|
|
---
|
2003-02-07 17:13:53 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
Some have suggested adding a new builtin instead of extending the
|
|
|
|
|
syntax of the language. For example:
|
2003-02-07 15:18:45 -05:00
|
|
|
|
|
2003-02-11 09:59:18 -05:00
|
|
|
|
cond(<condition>, <expression1>, <expression2>)
|
2003-02-07 15:18:45 -05:00
|
|
|
|
|
|
|
|
|
This won't work the way a syntax extension will because both
|
|
|
|
|
expression1 and expression2 must be evaluated before the function
|
|
|
|
|
is called. There's no way to short-circuit the expression
|
2003-02-11 09:59:18 -05:00
|
|
|
|
evaluation. It could work if 'cond' (or some other name) were
|
|
|
|
|
made a keyword, but that has all the disadvantages of adding a new
|
|
|
|
|
keyword, plus confusing syntax: it *looks* like a function call so
|
|
|
|
|
a casual reader might expect both <expression1> and <expression2>
|
|
|
|
|
to be evaluated.
|
2003-02-07 12:03:31 -05:00
|
|
|
|
|
2003-02-07 17:29:39 -05:00
|
|
|
|
|
2003-02-11 00:43:56 -05:00
|
|
|
|
Summary of the Current State of the Discussion
|
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
Groups are falling into one of three camps:
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
1. Adopt a ternary operator built using punctuation characters:
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
<condition> ? <expression1> : <expression2>
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
2. Adopt a ternary operator built using new or existing keywords.
|
|
|
|
|
The leading examples are:
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
<condition> then <expression1> else <expression2>
|
|
|
|
|
(if <condition>: <expression1> else: <expression2>)
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
3. Do nothing.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
The first two positions are relatively similar.
|
|
|
|
|
|
|
|
|
|
Some find that any form of punctuation makes the language more
|
2003-02-11 09:59:18 -05:00
|
|
|
|
cryptic. Others find that punctuation style is appropriate for
|
|
|
|
|
expressions rather than statements and helps avoid a COBOL style:
|
|
|
|
|
3 plus 4 times 5.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
Adapting existing keywords attempts to improve on punctuation
|
|
|
|
|
through explicit meaning and a more tidy appearance. The downside
|
|
|
|
|
is some loss of the economy-of-expression provided by punctuation
|
|
|
|
|
operators. The other downside is that it creates some degree of
|
|
|
|
|
confusion between the two meanings and two usages of the keywords.
|
|
|
|
|
|
2003-02-13 01:59:58 -05:00
|
|
|
|
Those difficulties are overcome by options which introduce new
|
|
|
|
|
keywords which take more effort to implement.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
The last position is doing nothing. Arguments in favor include
|
|
|
|
|
keeping the language simple and concise; maintaining backwards
|
2003-02-13 01:59:58 -05:00
|
|
|
|
compatibility; and that any every use case can already be already
|
2003-02-11 09:59:18 -05:00
|
|
|
|
expressed in terms of "if" and "else". Lambda expressions are an
|
|
|
|
|
exception as they require the conditional to be factored out into
|
|
|
|
|
a separate function definition.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
The arguments against doing nothing are that the other choices
|
|
|
|
|
allow greater economy of expression and that current practices
|
|
|
|
|
show a propensity for erroneous uses of "and", "or", or one their
|
2003-02-13 01:59:58 -05:00
|
|
|
|
more complex, less visually unappealing workarounds.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Short-Circuit Behavior
|
|
|
|
|
|
2003-02-11 09:59:18 -05:00
|
|
|
|
The principal difference between the ternary operator and the
|
|
|
|
|
cond() function is that the latter provides an expression form but
|
|
|
|
|
does not provide short-circuit evaluation.
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
Short-circuit evaluation is desirable on three occasions:
|
|
|
|
|
|
|
|
|
|
1. When an expression has side-effects
|
|
|
|
|
2. When one or both of the expressions are resource intensive
|
|
|
|
|
3. When the condition serves as a guard for the validity of the
|
|
|
|
|
expression.
|
|
|
|
|
|
|
|
|
|
# Example where all three reasons apply
|
2003-02-13 01:59:58 -05:00
|
|
|
|
data = isinstance(source, file) ? source.readlines()
|
|
|
|
|
: source.split()
|
2003-02-11 00:43:56 -05:00
|
|
|
|
|
|
|
|
|
1. readlines() moves the file pointer
|
|
|
|
|
2. for long sources, both alternatives take time
|
|
|
|
|
3. split() is only valid for strings and readlines() is only
|
|
|
|
|
valid for file objects.
|
|
|
|
|
|
2003-02-13 10:01:53 -05:00
|
|
|
|
Supporters of a cond() function point out that the need for
|
2003-02-11 09:59:18 -05:00
|
|
|
|
short-circuit evaluation is rare. Scanning through existing code
|
|
|
|
|
directories, they found that if/else did not occur often; and of
|
|
|
|
|
those only a few contained expressions that could be helped by
|
|
|
|
|
cond() or a ternary operator; and that most of those had no need
|
|
|
|
|
for short-circuit evaluation. Hence, cond() would suffice for
|
|
|
|
|
most needs and would spare efforts to alter the syntax of the
|
|
|
|
|
language.
|
|
|
|
|
|
|
|
|
|
More supporting evidence comes from scans of C code bases which
|
|
|
|
|
show that its ternary operator used very rarely (as a percentage
|
|
|
|
|
of lines of code).
|
|
|
|
|
|
|
|
|
|
A counter point to that analysis is that the availability of a
|
|
|
|
|
ternary operator helped the programmer in every case because it
|
|
|
|
|
spared the need to search for side-effects. Further, it would
|
|
|
|
|
preclude errors arising from distant modifications which introduce
|
|
|
|
|
side-effects. The latter case has become more of a reality with
|
|
|
|
|
the advent of properties where even attribute access can be given
|
|
|
|
|
side-effects.
|
|
|
|
|
|
|
|
|
|
The BDFL's position is that short-circuit behavior is essential
|
|
|
|
|
for an if-then-else construct to be added to the language.
|
2003-02-07 21:12:43 -05:00
|
|
|
|
|
2003-02-07 17:29:39 -05:00
|
|
|
|
|
2003-02-07 12:03:31 -05:00
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
sentence-end-double-space: t
|
|
|
|
|
fill-column: 70
|
|
|
|
|
End:
|