[Edu-sig] Why colon?
Guido van Rossum
guido@python.org
Fri, 15 Mar 2002 12:11:52 -0500
> 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.
>
> Thanks,
> Jeff Sandys
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.
--Guido van Rossum (home page: http://www.python.org/~guido/)