A Suggestion for Python Colon Syntax

William Djaja Tjokroaminata billtj at y.glue.umd.edu
Thu Dec 21 11:48:17 EST 2000


Hi all,

I have been using Python for some time, and I have a suggestion for the
Python Colon (:) syntax.

I have used C/C++, Tcl/Tk, Perl, etc., and really one thing that I like
about Python is its indentation-based syntax.  Sure, we cannot write
one-liners like what Perl is famous for, but the code layout looks
beautiful, because I don't have to type { and } blocks which result
in various formatting conventions in C/C++.  Also the fact that we don't
have to write ( and ) after if, for, etc., is wonderful, because the code
has less "punctuation" and looks more clean.

However, a lot of time when I ran my Python program, I got syntax error,
because I forgot to put colon (:) after my if (or for, etc.) clause.  My
question is, is the colon really necessary, syntactically, in
Python?  Because Python is line-and-indentation-based, can we just
eliminate the colons after the if, for, etc., clause?

My suggestion is as follows:

1) If the if (or for, etc.) clause is followed by a block, then the colon
is optional (for backward compatibility):

    if condition is true
        statement_1
        statement_2
        ....

2) If the if (or for, etc.) clause is followed only by a single statement,
then the colon can be used to write a single line of code:

    if condition is true: statement_1
    next_statement
    ....

Any comments, anyone?

Regards,

Bill



More information about the Python-list mailing list