[Python-Dev] Trinary Operators
Guido van Rossum
guido@python.org
Thu, 06 Feb 2003 12:02:06 -0500
> Notwithstanding which, I occasionally wish that Python had an
> if-then-else operator.
Me too, on odd-numbered days. (On even-numbered days I think the
addition to the learning curve isn't worth the savings in typing.)
> 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).
It's elif.
> 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".
I think I've seen the suggestion
x = (y if y>z else z)
:-)
--Guido van Rossum (home page: http://www.python.org/~guido/)