tags:

views:

1592

answers:

5

What is the purpose of the colon before a block in Python?

Example:

if n == 0:
    print "The end"
+8  A: 

So you can put the 'then' clause on the same line:


if n == 0: print "The end"
else: print "Not the end"
for i in range(5): print i
Adam Rosenfield
Never do that! This is totally unreadable!
pi
this doesn't explain why its not optional though
brbob
+18  A: 

The colon is there to declare the start of an indented block.

Technically, it's not necessary; you could just indent and de-indent when the block is done. However, based on the EIBTI Python koan (explicit is better than implicit), I believe that Guido deliberately made the colon obligatory, so any statement that should be followed by indented code ends in a colon. (It also allows one-liners if you continue after the colon, but this style is not in wide use.)

It also makes easier the work of syntax-aware auto-indenting editors, which also counted in the decision.

edit

This question turns out to be a Python FAQ, and I found one of its answers by Guido here.

PS Thanks to ShaChris23 for supplying a correction to the Python FAQ URL; the Python site has been reorganized since this answer was first written.

ΤΖΩΤΖΙΟΥ
The above Python FAQ link doesn't work anymore. This one works though: http://docs.python.org/faq/design.html#why-are-colons-required-for-the-if-while-def-class-statements
ShaChris23
@ShaChris23: thank you very much for supplying a more recent URL for the FAQ.
ΤΖΩΤΖΙΟΥ
No problem. ;-)
ShaChris23
+6  A: 

Three reasons:

  1. To increase readability. The colon helps the code flow into the following indented block.
  2. To help text editors/IDEs, they can automatically indent the next line if the previous line ended with a colon.
  3. To make parsing by python slightly easier.
Ryan
+4  A: 

Consider the following list of things to buy from the grocery store, written in Pewprikanese.

pewkah
lalala
    chunkykachoo
    pewpewpew
skunkybacon

When I read that, I'm confused, Are chunkykachoo and pewpewpew a kind of lalala? Or what if chunkykachoo and pewpewpew are indented just because they are special items?

Now see what happens when my Pewprikanese friend add a colon to help me parse the list better: (<-- like this)

pewkah
lalala:   (<-- see this colon)
    chunkykachoo
    pewpewpew
skunkybacon

Now it's clear that chunkykachoo and pewpewpew are a kind of lalala.

Let's say there is a person who's starting to learn Python, which happens to be her first programming language to learn. Without colons, there's a considerable probability that she's going to keep thinking "this lines are indented because this lines are like special items.", and it could take a while to realize that that's not the best way to think about indentation.

RamyenHead
I almost want to upvote that for your hilarious examples... but the other answers are much stronger. Still, thanks for the smile!
Gabriel Hurley
+4  A: 

As far as I know, it's an intentional design to make it more obvious, that the reader should expect an indentation after the colon.

It also makes constructs like this possible:

if expression: action()
code_continues()

Note (as a commenter did) that this is not exactly the shining gold standard of good Python style. It would be far better to have a blank, there:

if expression: action()

code_continues()

to avoid confusion. I just wanted to make it clear, with the first example, that it's possible to write like that, since having the code for the if immediately following the colon makes it possible for the compiler to understand that the next line should not be indented.

unwind
I hate formatting like that. I much rather a new line.
Dominic Bou-Samra