PEP 308: Pep Update
Steven Taschuk
staschuk at telusplanet.net
Thu Feb 27 13:21:11 EST 2003
Quoth Norman Petry:
> On Thu, 27 Feb 2003 14:51:03 +1300, Greg Ewing (using news.cis.dfn.de)
> wrote:
[...deprecation of ?: in C style guides...]
> > But is that because of the idea of a conditional expression in general,
> > or just the particular syntax used in C?
>
> Probably the latter.
>
> I think that the C syntax for the ternary is very poor, mostly because
> there is nothing intuitively obvious about it.
>
> C? trueValue : falseValue
>
> is a bad syntax in general, but _particularly_ for Python because (imho):
[...]
A well-argued post.
I'd add that one more problem with ?: is that these characters are
not visually salient, or at least, not as salient as, for example,
&& and ||. Thus the precedence structure of a complex expression
involving these operators is not visually obvious. This is
another reason to use a keywordy syntax for the ternary operator
in Python; such a syntax puts the operator in about the same
visual class as 'and' and 'or', more visually salient than the
higher-precedence arithmetic operators.
(Note that this argument in favour of a keywordy syntax does not
reduce to the absurd replacement of + with 'add'.)
PEP 308's current proposal, (if C: x else: y), is unfortunate in
this respect: the colon after the condition is not as visually
salient as its significance requires. The analogy to statement-if
syntax is not helpful: for statement-if it is the newline and
indentation which make the break between the condition and the
true-branch visually salient; the colon appears not as a
separator, but as some kind of visual aid in reading across the
real separator, the newline and indentation. (That statement-if
allows a one-statement branch to appear on the same line,
separated only by :, is imho a wart, for exactly this reason.)
Your favored syntax, x if C else y, passes this test handily.
I've snipped most of your points, since I agree with most of them.
I disagree, however, with point 4:
> 4) it is somewhat illogical to have the condition appear at the beginning
> of the expression, since it is one of the two _values_ that gets assigned
> to the result. Python arithmetic expressions normally use infix
> operators, with the type of the result often being the same/similar to the
> value that begins the expression. While there are of course many
> exceptions to this, a better syntax would be one which at least suggests
> the result type to the reader just by looking at the beginning of the
> ternary expression. For similar reasons, it would be equally bad to begin
> the ternary expression with a keyword (result = if...?!)
I prefer that expressions appear, left-to-right, in the order they
are evaluated. (For one thing, this makes it easier to reason
about side effects.) This is especially so when some of the
operators have important control-flow semantics, as the ternary
operator does.
Imagine, for example, a short-circuiting 'and' which evaluates its
*second* argument first; that way lies madness. You *can* read
such a thing left-to-right; you could even name it 'onlyif' to
make it sound "natural" when spoken aloud. But the left-to-right
reading order is misleading as to the evaluation order. (This
might be what other posters meant by their claim that x if C else
y "can't be read left-to-right".)
Since the ternary operator evaluates the condition first, then one
of the values, this argument supports putting the condition before
the values.
(List comprehensions are a notable counterprecedent.)
> The objections to the 'x if C else y' syntax have all been very weak,
> imho. Most people who have criticised the proposal have done little more
> than to point out that the condition appears in the middle of the
> expression (unlike C, which presumably does things the "right" way). Of
> course, this is no argument at all -- it's merely restating what the
> proposed syntax *is*, without explaining why there's a problem with doing
> it that way. [...]
I hope my comments above address this lack somewhat.
--
Steven Taschuk staschuk at telusplanet.net
"What I find most baffling about that song is that it was not a hit."
-- Tony Dylan Davis (CKUA)
More information about the Python-list
mailing list