[Python-ideas] Making colons optional?

Stephen J. Turnbull stephen at xemacs.org
Fri Feb 6 17:21:46 CET 2009

Riobard Zhan writes:

 > It is pretty clear that colon-supporters do not pay attention to the  
 > inconsistency of semicolons being optional and colons being mandatory  
 > that I mentioned,

The inconsistency is quite small.  In fact, in English colons and
semicolons have both different syntax and different semantics, and the
same is true of Python.  In English, the semicolon expresses a
conjunction of two not necessarily related sentences.  Python is the
same, except that since we all run von Neumann machines, conjunction
implies sequencing.  The colon, on the other hand, represents a
relationship of some kind.  A native-speaker simply doesn't write
things like "The sky is blue: mesons are composed of two quarks."
(Well, the coiner of the work "quark" might, which is the exception
that proves the rule.)  And the same is true in Python; the code
fragment preceding the colon controls the statement (suite) following
it.  Thus, the apparent isomorphism of the semantics is quite
superficial, almost syntactic itself.

Syntactically, in English a semicolon takes two sentences to conjoin.
A colon, on the other hand, must have a full sentence on one side or
the other, but the other side may be almost anything.[1]  Similarly in
Python: both what precedes the semicolon and what follows it must be
complete statements, while what precedes the colon must be a fragment,
and what follows it is a complete statement (suite).  So the colon and
semicolon do not have isomorphic syntax, either.

In fact, the semicolon is indeed redundant, as it is precisely
equivalent to a newline + the current level of indentation.  This kind
of redundancy is quite unusual in Python, so I wonder if the BDFL
might regret having permitted semicolons at all, as he has expressed
regret for allowing the extended assignment operators (eg, +=).

 > and try to argue with me using a set of reasons why  
 > they love colons when in fact the same reasons would obviously make  
 > them love semicolons, and ignore the fact that they are used to colons  
 > not because it is a better idea but they are never allowed to omit.

I think that

    if condition:
        act (appropriately)

is more readable than

    if (condition)
        act (appropriately);


    if condition
        act (appropriately)

In C, the visual parallelism of the control structure and the function
call is spurious, and the trailing semicolon actually terminates the
f statement, which is not the way I really think about it.

In colon-less Python, the control fragment is insufficiently divided
from the controlled suite.  Just as in English, a bit of redundancy to
reflect the hierarchical relationship between the control structure
and the controlled suite is useful.

In Python, the same character is used, which is helpful to those whose
native languages use colons.  This is indeed *good* design.  It
expresses, and emphasizes, the hierarchical structure of the control
construct, and does so using a syntax that any English-speaker (at
least) will recognize as analogous.

I don't expect to convince you that the required colon is good design,
but I think it puts paid to the notion that colons and semicolons are
isomorphic.  It is not true that what we conclude about semicolons is
necessarily what we should conclude about colons.

[1]  That's not entirely true.  It is possible to write something like

    Required colon: an idea which is still correct.

which does not contain any complete sentence, and where the colon is
actually a copula.  But even then, it's a relationship.

More information about the Python-ideas mailing list