[Python-Dev] Trinary Operators
Andrew Koenig
ark@research.att.com
06 Feb 2003 11:48:01 -0500
Greg> I won't claim to channel Guido, but C's ?: operator is ugly,
Greg> obscure, and weird. Just because it has been copied by Java and
Greg> Perl does not mean it is a good thing. The fact that Python
Greg> does not ape C is a *feature*.
Notwithstanding which, I occasionally wish that Python had an
if-then-else operator.
I personally like the way Algol 68 does it, although I acknowledge
that Python couldn't really do it the same way because Algol 68
does not distinguish between statements and expressions.
Algol 68 always uses closing-delimiter keywords to go with the
corresponding opening-delimiter keywords, so an if-expression
looks like this:
if ... then ... fi
if ... then ... else ... fi
if ... then ... elif ... then ... fi
if ... then ... elif ... then ... else ... fi
and so on (I think it's spelled "elif", but it might be "elsf" --
it's been a long time since I studied Algol 68).
What I find particularly nice is that Algol 68 allows "if" to be
abbreviated as "(", "fi" as ")", "then" or "else" as "|", and
"elif" as "|:".
So where a C programmer would write
x = (y>z? y: z);
an Algol 68 programmer would write
x := if y>z then y else z fi;
or, equivalently
x := (y>z | y | z);
Of course, using | this way works only because Algol 68 uses
different symbols for "and" and "or".
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark