single-line terinary operators considered harmful

Stephen Horne intentionally at
Wed Mar 5 12:06:46 CET 2003

On Wed, 5 Mar 2003 02:57:08 +0000, "Clark C. Evans"
<cce at> wrote:

>On Wed, Mar 05, 2003 at 02:14:06AM +0000, Dennis Reinhardt wrote:
>| The weakness is that Python has a "dangling else" ambiguity which requires
>| indentation to disambiguate (i.e. the if-else must occupy two lines
>| minimum).
>Why is this a weakness?  I like indentation.  I use Python exactly
>beacuse it uses indentation to show me structure.  The biggest problem
>I have with all of the 'terinary' options is that they are 
>single-line solutions.
>The only solution that the terinary operator needs to solve
>is a way to provide for conditional logic within an expression
>intead of forcing the programmer to use statements.

If you are literally building the source automatically by inserting
bits of expressions into a larger chunk of code (I assume that Zope is
doing this, and I also have some code which does this) then having to
obey indentation rules for the inserted expression is a nightmare. You
don't know what the indentation level was for the statement you are
inserting into when you write the expression, which may easily be
inserted into many different pieces of code.

Of course, this is a specialist issue and the difficulty of writing
code generators is not a good argument for making language design
decisions. Besides, if it became that serious an issue, someone could
always write a Python-source-manipulation library which automatically
obeyed indentation rules to preserve the intended meaning of
insertions. Not easy, but achievable.

So is there another argument?

Well - the programmer is not forced to do all block structure by
indentation in Python. There are aspects that are left to the
programmers judgement. For instance, you can already write...

  if c : do_x; do_y; do_z

In an expression, structuring by indentation does not exist at all at
the moment. Structuring within expressions is done by precedence and
associativity rules, and by parentheses. If indentation is to be used
for structuring within expressions, then new indentation rules are
probably needed - something like Haskells offside rule (which I
mentioned recently in another thread).

But is it even natural to ALWAYS use indentation for a particular
structure in an expression? I don't think it is.

No-one would write a language which insists that basic arithmetic
operators are structured with indentation. This can be useful,
however, even with parentheses or precedence making the real decisions
- for instance...

  a = (  (  (some_long_expression)
          * (some_long_expression))
       + (  (some_long_expression)
          * (some_long_expression)))

...but that's no reason to force it on everyone in every case.

While it is by no means as clear cut a case, I do believe essentially
the same logic applies to conditional operators. Indentation may be a
very useful thing in ensuring something is readable, but at the end of
the day that should be left to the individual programmers judgement.
Absolute dogmatic rules don't always make the right decision.

As it happens, I tend to think of the else problem in existing if
statements as a (very minor) wart. I see no problem with readability
in something like...

  if b != 0 : print "a/b = ", a/b ;; else : print "div by zero"

(using ';;' as a hypothetical 'stronger' end-of-statement operator)

This would particularly be the case with many similar if-statements in
sequence - the vertical alignment between clauses can emphasize both
the pattern and the exceptions, and can dramatically help readability,
but that benefit is largely lost for a sequence of distinct multiline

While I can do that in C++ (still keeping the braces, which I really
don't like to omit) I've lost that capability in Python. It's
certainly a very good trade overall, but I don't want to see even a
very minor wart replicated unnecessarily.

In a kind of 20/20 hindsight + personal preference thing, if I was
writing Python I'd have probably kept braces as an option for block
structuring when indentation didn't seem appropriate (or for those who
want to combine both) - just as Guido kept ';' as an optional
statement terminator. Lists and dictionaries would both use '[]', but
with an initial keyword to say which was which (or perhaps using the
colons in a dictionary to disambiguate). If braces were used for a
multi-line block, it would still have to obey indentation rules as

I suspect that even Guido might be tempted if he were rewriting
Python, not so much to put an else on the same line but because a lot
of people complain about structuring by indentation from time to time.
Yes - there are even people (though emphatically NOT me) who see all
structuring by indentation as a wart.

steve at ninereeds dot fsnet dot co dot uk

More information about the Python-list mailing list