Defending the ternary operator

Kaz Kylheku kaz at
Mon Feb 10 21:31:43 CET 2003

<cribeiro at> wrote in message news:<mailman.1044678564.8549.python-list at>...
> Why is a ternary operator needed? The main reason is that it allows the
> programmer to clearly express his intentions.

Once upon a time a programming language was invented which
artificially divided computation into statements, which directed the
control flow, and expressions, which evaluated formulas. The creators
looked upon the language, and thanks to their inexperience saw that it
was not bad.

In reality, this division into statements and expressions is nonsense.
You want evaluation control in expressions, which is why the C
language has operators like comma, &&, || and the ternary operator.

The real solution is not to divide the language into expressions and
statements, just have expressions. Then you don't run into stupid
problems, like not being able to use a statement where an expression
is expected. The role of statements is usurped by constructs which
are, syntactically, expressions, but which like the ternary operator
have special evaluation rules.

More information about the Python-list mailing list