Why aren't colons optional?

Edward K. Ream edream at tds.net
Sat Jan 19 21:27:39 EST 2002


Hans Nowak wrote:

> Note the colons in your own post. :-)  They are not really
> necessary for understanding what you wrote, but they make
> the sentence easier to read. Same with Python. The colon
> is not necessary for parsing if it's followed by a code
> block, but it increases readability. It's there for
> human readers.

My original questions were technical in nature. Let me repeat the second
question:

"Can anyone provide an example of similar code that Python can't parse
without colons?"

To be as clear as possible, I am not asking about what the present
parser will do; it will object if the colons following def, if, etc. are
omitted.  I am really asking a question about the grammar. It looks like
any reasonable parser should able to parse Python with optional colons.
Indeed, it is always obvious where expressions end, so colons that
separate an "expression" or "expression list" from a "suite" in the
grammar should be optional. Nor do I believe colons are required for
robust error recovery.

As to readability. Colons following def, if, etc., no more add to
readability than do trailing semicolons.  The only syntax error I
regularly make is to forget a trailing colon. After all, newlines
clearly terminate statements, so it is easy to overlook that a redundant
colon is missing.

Trailing colons seem useless. Newlines terminate expressions! The only
thing that would change this opinion is 1) an example of correct code
that could not be parsed properly without a trailing semicolon or 2) an
example of incorrect code for which proper error recovery could not be
performed without a trailing colon.  I do not believe either
counterexample exists, and I could be completely wrong. That's why I am
asking the experts.

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edream at tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------



More information about the Python-list mailing list