Why aren't colons optional?
sheershion at mailexpire.com
Sun Jan 20 19:57:21 CET 2002
Hans Nowak wrote:
> "Edward K. Ream" wrote:
>> Why are colons required after def, elif, else, except, finally,
>> for, if, try and while? One would think the colon would be
>> optional when the colon is followed by a newline.
>> Obviously, colons are required in: ^
>> if a == 1 : b = c
>> elif a == 2 : b = d
>> else : b = e
>> My question is: why isn't the following valid?
> 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.
Not _only_ for human readers: some editors/IDEs use trailing colons to
provide automatic indentation. When so, the 'redundant' colon is
actually free, in terms of keystrokes.
>From the point of view of parsing a language, simple rules are better:
parsing is faster and when things go wrong it's easier to come up with
helpful error message (although Python still could use some improvement
in that area).
A particularly horrible example of how *not* to design a language
LiveScript). As with Java and C, EcmaScript requires that each
statement is terminated by a semicolon, but with a curious exception:
if the semicolon would have been the last character on the line, and
some other conditions are met, the semicolon may be omitted.
What a horrible inconsistency! Worse still, newlines aren't just
whitespace anymore (which is discarded by the lexer) but have become
tokens in their own right. Most of the time those newline-tokens are
meaningless garbage in the token stream, but the grammar still has to
allow for them. What a horrible, horrible mess! And imagine what this
does for the accuracy and helpfulness of any error message, which is
always a concern with parsers.
And all that for... Yes, for what, exactly? To save a few keystrokes?
To satisfy a few script-kiddies who might have started out using
something like Visual Basic and can't adjust to a free-format syntax?
It just makes no sense whatsoever, so it's probably a 'political'
So don't complain or question, but *cherish* the consistency of Python.
It is a great bonus.
More information about the Python-list