atom feed2 messages in org.python.edu-sig[Edu-sig] Why colon?
FromSent OnAttachments
Jeff SandysMar 14, 2002 3:54 pm 
Guido van RossumMar 15, 2002 9:11 am 
Subject:[Edu-sig] Why colon?
From:Guido van Rossum (gui@python.org)
Date:Mar 15, 2002 9:11:30 am
List:org.python.edu-sig

What is the use of the colon in def and if? It only seems to signal, indent the next line, but if the next line is indented anyway, what is the colon's purpose?

We have a semi-colon to put multiple statements on a line, why not use that for a single-line if,

if x > 0 ; print "positive" else ; print "negative"

Then when we use multiple lines we still indent,

if x > 0 print "positive" else print "negative"

It is a common error of new Python programmers to leave out the colon after def, if and else, and I can't explain why the colon is needed.

This is a FAQ (but anyway, it's too late to change):

http://www.python.org/cgi-bin/faqw.py?query=colon&querytype=simple&casefold=yes&req=search

The colon is required primarily to enhance readability (one of the results of the expirimental ABC language). Consider this:

if a==b

print a

versus

if a==b:

print a

Notice how the second one is slightly easier to read. Notice further how a colon sets off the example in the second line of this FAQ answer; it's a standard usage in English. Finally, the colon makes it easier for editors with syntax highlighting.