I would like to understand whether it would be possible to change the syntax of Python making the colons of compound statements optional when they are at the end of the line, apart from whitespace and comments. E.g.:
if so() print("yes") else print("no")
Would this change break existing Python code? Would there be ambiguous constructs? Would the parser become exceedingly bad at diagnosing some kind of error? Would there be another problem I can’t think of now?
Of course, I know the argument that the colons makes the code easier to read. I think this is debatable, but I absolutely don’t want to discuss opinions here. My question is about possible technical problems.
According to the syntax of compound statements in the documentation there is always a ":"
before a suite
, where suite
is defined as
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
Taking as an example the if
statement
if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite]
the syntax could, apparently, be modified like this:
suite ::= ":" stmt_list NEWLINE | [":"] NEWLINE INDENT statement+ DEDENT if_stmt ::= "if" expression suite ("elif" expression suite)* ["else" suite]
In the Grammar file of the CPython source code (also available here) things are a bit different, and a bit complicated by the optional type comment after the colon in some constructs, but all in all it seems to me that it should be possible to modify the syntax similarly to the above.
What technical impediments would there be?
Update: The following could be a more LL(1)-friendly syntax for suite
:
suite ::= colon_suite | indented_suite colon_suite ::= ":" (stmt_list NEWLINE | indented_suite) indented_suite ::= NEWLINE INDENT statement+ DEDENT
Advertisement
Answer
Lookahead technicalities aside, the grammar would certainly not become ambiguous: all you’re doing is interpolating a :
at the end of any line (before any comment) where doing so avoids a syntax error (so not after lambda
, where one would be necessary but not sufficient). In particular, no suite introduction can be extended over multiple lines except with backslashes or inside bracketing characters (one can’t write for
and in
, or except
and as
, on separate logical lines, for instance).
The only obvious candidates for an impaired diagnostic would be confusion with a ternary conditional or generator expression, but that’s very weak because of the expression before the if
/for
(and the parenthesis for the latter). There would probably be no technical impediments at all.