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

Question:

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

Example:

if n == 0:
    print "The end"
Asked By: Joshua Swink

||

Answers:

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 Python koan “explicit is better than implicit” (EIBTI), 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 the work of syntax-aware auto-indenting editors easier, which also counted in the decision.


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

Why are colons required for the if/while/def/class statements?

The colon is required primarily to enhance readability (one of the results of the experimental 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 this FAQ answer; it’s a standard usage in English.

Another minor reason is that the colon makes it easier for editors with syntax highlighting; they can look for colons to decide when indentation needs to be increased instead of having to do a more elaborate parsing of the program text.

Answered By: tzot

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.
Answered By: Ryan

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.

Answered By: Yoo

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()

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.

Answered By: unwind

According to Guido Van Rossum, the Python inventor, the idea of using a colon to make the structure more apparent is inspired by earlier experiments with a Python predecessor, ABC language, which also targeted the beginners. Apparently, on their early tests, beginner learners progressed faster with colon than without it. Read the whole story at Guido’s post python history blog.

http://python-history.blogspot.com/2009/02/early-language-design-and-development.html

And yes, the colon is useful in one-liners and is less annoying than the semicolon. Also style guide for long time recommended break on several lines only when it ends with a binary operator

x = (23 + 
     24 + 
     33)

Addition of colon made compound statement look the same way for greater style uniformity.

There is a ‘colonless’ encoding for CPython as well as colon-less dialect, called cobra. Those did not pick up.

Answered By: Serge

The colon is a complete annoyance. If you need to indent after an ‘if’ or a ‘for’, then just look for the ‘if’ or the ‘for’.

Cmon’, all this rationalization. The ‘spin’ language for the propeller chip easily overcomes this issue.

PLEASE, let’s make the colon optional, and get on with some logical programming.

Answered By: Jim K

"… After early user testing without the colon, it was discovered that the meaning of the indentation was unclear to beginners being taught the first steps of programming. The addition of the colon clarified it significantly: the colon somehow draws attention to what follows and ties the phrases before and after it together in just the right way…"

Guido, You could have chosen a new group of beginners. Admit it- you were just feeling lazy that day. Getting rid of the semicolon and leaving the colon is like performing an appendectomy and leaving your wristwatch in the patient’s belly.

Answered By: user20842769
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.