82

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

Example:

if n == 0:
    print "The end"
Joshua Swink
  • 3,380
  • 3
  • 29
  • 27
  • 23
    I found this colon very annoying when first learning Python: it seems inconsistent with the "space based syntax" philosophy of the entire project. None of the reasons people give are compelling, unless it is "Oops, my bad: it's too late to change." – eric May 18 '15 at 13:08
  • 4
    Well, that's precisely what [Guido said](http://markmail.org/message/ve7mwqxhci4pm6lw): "*it's too late to change*". – Gabriel Jan 21 '17 at 15:55
  • 7
    @Gabriel -- Your comment is misleading. The substantive content of Guido's answer at the link you provided expounds the decision to implement the colon, indicating that it was and still is a valid choice. "It's too late to change" appears as minor parenthetical content, and the full quote is actually, "but anyway, it's too late to change"; the "but anyway" portion of the quote, which you left out in your comment, clearly indicates that it was merely an observation or matter of fact on Guido's part. – rory.ap Mar 31 '17 at 11:47
  • 3
    Without that context, your comment is misleading because it appears to bolster neuronet's point which is entirely predicated on the inclusion of the colon being a mistake that can no longer be changed, which it clearly isn't. Future readers of this post may quickly read your comment (ignoring the answer(s) below) and draw a wrong conclusion. – rory.ap Mar 31 '17 at 11:47
  • 1
    I don't' think my comment is misleading, and I also don't pretend to know what Guido thinks. It's been 15 years since Guido's comment. I'm thinking the colon is not going away anytime soon. Cheers. – Gabriel Mar 31 '17 at 12:17

5 Answers5

76

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.

poke
  • 369,085
  • 72
  • 557
  • 602
tzot
  • 92,761
  • 29
  • 141
  • 204
  • 1
    Yeah, but (1) oneliners are a bad idea. (PyLint criticizes them, the FAQ does not even name them as an argument) Editors (2) are advanced enough today that more elaborate parsing (like PyLint does) can be required. Readability (3) is mainly higher in the FAQ, because Pygments chokes on the wrong syntax. That it is standard usage in English (4) might be the best point for having this construction. The best point against changing it, must be as Guido put it: "its too late to change". – Bengt Aug 19 '12 at 23:52
  • 14
    Ah. Explicit is better than implicit. You know what else is explicit? Block closing delimiters. Cult programming. – Mark Gerolimatos Jun 29 '14 at 22:07
  • 2
    @MarkGerolimatos block closing delimiters are explicit, but so is de-indentation. – Andy Feb 17 '17 at 18:37
  • 1
    @andy de-intentation is FAR from explicit: tabs versus spaces, one fewer / one too many space (very hard to see with smaller fonts and variable-spaced fonts), lack of "find the end of this block" capabilities in non-Python-aware editors. These are likely the reasons that The Google put curly braces "back" in to Go(Lang), when they were originally going to use the Python indentation model. – Mark Gerolimatos Feb 27 '17 at 08:44
  • @MarkGerolimatos perhaps explicit is a bad word here. Python code *is* unambiguous, otherwise Python interpreters wouldn't be able to function properly. But you're right that it's a lot harder for a program to find the end of a Python block correctly, and that whitespace isn't *visually* explicit. – Andy Feb 28 '17 at 14:56
  • 5
    The first example actually seems easier to read, at least to me. And I don't think syntax highlighting would be a problem with modern editors. – user76284 Jul 31 '18 at 16:53
  • Indentation is explicit (though the necessary rules for analyzing indentation gets complicated). If you consider lexical analysis and parsing to be distinct processes (CPython blends them together to some extent), it's the job of lexical analysis to turn line-initial whitespace into one or more `INDENT` tokens, which the parser then uses to build the AST. `INDENT` tokens are simple: one token indicates one level of indentation. It's the fact that an arbitrary amount of whitespace in the *first* indented line of a block establishes the "size" of an `INDENT` token that makes things confusing. – chepner Dec 22 '22 at 21:03
  • Less flexibility probably would have been better. An `INDENT` token being defined as four spaces (no more, no less, no tabs) might not have been a terrible idea when such a definition was still an option. – chepner Dec 22 '22 at 21:04
  • Also, I don't think the colon is optional if you intend to support single-line statements like `if condition: statement`. Such may be frowned up *now*, but I don't think that was the original expectation, or why would they have been allowed in the first place. – chepner Dec 22 '22 at 21:12
27

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.

Yoo
  • 17,526
  • 6
  • 41
  • 47
  • 2
    I almost want to upvote that for your hilarious examples... but the other answers are much stronger. Still, thanks for the smile! – Gabriel Hurley Sep 23 '09 at 08:16
  • 12
    the thing is that the indents are required in python, so this example doesn't really wash for me. If the colons are required then the indenting should be optional I say :-) why have two indications of the same thing? Isn't that more complex than just having one? – Sam Joseph Feb 14 '12 at 17:06
  • 8
    +Sam Joseph: The colon marks the start, not the end. So you couldn't make indenting optional. Only colons could be optional. – Mark Sep 22 '16 at 21:20
  • Unfortunately, the dialect of Pewprikanese I’m most familiar with uses colons as a marker for plurals. Thus, in your second example, the colon does not help me parse the list better at all. Colons don’t have any more universal meaning than any other linguistic symbol. Actually, you are relying on the familiarity of most readers with its common use in English and other dominant languages. Now, that being taken into consideration, one must agree that, in those languages, indentation serves exactly the same purpose as the one Python uses it for. – Philippe-André Lorin Aug 24 '23 at 15:57
  • … For examples of languages where colon-shaped symbols have a whole different meaning, see https://en.wikipedia.org/wiki/Colon_(letter) – Philippe-André Lorin Aug 24 '23 at 15:59
17

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
  • 15,016
  • 6
  • 48
  • 50
  • 1
    +1 - "To increase readability" is the correct reason. But as I'm drafting my own language, I wonder, why is the burden on the writer to make the code readable? Syntax highlighting improves readability, but there's no requirement that the author bold certain portions of the code, for example. It seems to me that the IDE aught to handle giving that additional visual queue for where a block begins, just like it highlights certain keywords by coloring or bolding or whatevering them. And if the reader would rather do without or with other indicators/styles - they could change the skin. – ArtOfWarfare Oct 22 '13 at 12:49
  • 11
    I believe it is only more readable to people who are conditioned to expect it. For those new to Python, which is weird because it only uses spaces, the sudden use of a colon instead of just spacing doesn't make it any more readable, and only leads to mistakes. The only real reason is that it is too late to change it. Syntax parsers could easily work without the colon, come on people: IDEs can be sensitive to a little thing I like to call "carriage return." – eric May 18 '15 at 13:06
7

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Just want to note here that the ability to skip the new line seems to be the best justification for this piece of syntax as annoying as it may seem. I used this example for the justification: `def long_words(lst): return list(filter((lambda word: len(word) > 5), lst))` works as a one-liner for people who appreciate compactness of simple functions. – BNazaruk Dec 06 '21 at 02:48
  • 1
    If the colon was _optional_, one-liners would still be possible. – Martin Rust Jan 13 '23 at 15:31
  • Yes, the colon should be optional. The OP is correct and Guido and everyone else is wrong. (my two cents) – user3761340 Jul 07 '23 at 03:32
2

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

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.

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

I agree that mature programmers, in my experience, and myself got annoyed by unusual construct. But usually get used after a while and nowadays, python is often is among the first languages to learn.

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.

Serge
  • 3,387
  • 3
  • 16
  • 34
  • To me that indicates that someone disliked it so much they omitted it. They fact that cobra was not successful does not speak much to the viability of the optional colon. I'm not sure I understand how the break on several lines comment is related. Please explain. – user3761340 Jul 07 '23 at 03:36
  • I doubt the ABC test was very scientific. The colon seems ugly and unnecessary to me. I bet beginners, if they were left to their own accord and just typed what they thought made sense, would not think to add a colon. Of course, they also might not add a new line and indent either. It's not a huge deal, but every time I forget one, I get a little ruffled (lol). Also, if it is optional, don't we have the best of all worlds? – user3761340 Jul 07 '23 at 03:40
  • I have no data regaring ABC testing process, they explanation sounds good to me. Few would pay attention to indentation unless there is a bullet point or at the least a colon prior to. I suspect your was already exposed to other languages prior learning python. – Serge Jul 07 '23 at 16:16
  • In typography, for many spoken languages most of punctuation can be replaced with appropriate spacing. Small white space for comma, larger for full stop - anyway capitalization can show start of sentence. Question is usually clear from the word order. Italic or bold can be used instead of exclamation and so on. Would novels look prettier? Yes. Easier to read? I am not so sure and ready to tolerate some ugliness for sake of clarity (well, millennials, and advertisers might disagree). – Serge Jul 07 '23 at 16:20