28

This is really just a theoretical question by a python newbie who wants to understand more.

I keep forgetting the colon after the block initial statements in python. Those are what I mean:

  • for <variable> in <sequence>:
  • if <blah blah>:

My thought is that one reason I keep forgeting is, that they are de-facto implicit: colon or not, the statement ends with that line.

My question - which I ask in order to learn how python syntax works - is, whether the colon is really unnecessary? Were I to change the python syntax so that the colon is no longer necessary, would anything break? Would that make some statements ambiguous or impossible?

7
  • 4
    I think you do not understand the question, which is whether the colons are necessary for the syntax to work. Also, no matter what your answer is, it should also contain an explanation. Commented Jan 27, 2017 at 11:51
  • Probably, Can you rephrase parts of the question, so that I can benefit from your insight into this matter, maybe an example? You'd be the best judge, I think I'm clueless about your intention of asking. Do you mean it at the level of interpreter/compiler's parser? Thanks a bunch.
    – bhan sur
    Commented Jan 27, 2017 at 11:57
  • I don't know how to phrase it any better. My question basically is, if you could change all python syntax so that it no longer needs colon after if, else, while and so on. If you did that, would python still be language that can be used without ambiguity? Commented Jan 27, 2017 at 12:03
  • Got it! It's a question around python's syntax design decisions. Sorry, misunderstood it. Thank you for explaining.
    – bhan sur
    Commented Jan 27, 2017 at 12:05
  • Speculating. It is as if the line breaks are harder to detect by interpreters/parsers in practice or are there for readability. In LUA you can write if .. then .. end in a single line. So here in python then is substituted by two things a : and a required newline. One of these seems redundant.
    – bhan sur
    Commented Jan 27, 2017 at 12:23

4 Answers 4

12

Yes, the colon is required to disambiguate certain constructs. Consider, for example, if x - y < z: pass. Without the colon we cannot decide how to parse this without knowing the context of what x, y, and z are. if x: -y < z... is valid if x is boolean, if x - y < z: is valid otherwise.

As it's a very good idea for a programming language to not require you to execute an application up to the point you're compiling to be able to parse it, the colon is very required. You could drop it, but you'd need other ways to disambiguate.

11
  • 1
    Wait, you can have a statement after the colon on the same line? I was pretty sure it's not allowed. Commented Jan 27, 2017 at 12:00
  • 2
    It is permitted, but only with a linebreak after it.
    – Phoshi
    Commented Jan 27, 2017 at 12:14
  • Still a little confused. Is this allowed: if condition: print("Condition passed")\n allowed? The \n symbolises new line after the print statement. Commented Jan 27, 2017 at 12:17
  • 1
    Sure, just try it. Commented Jan 27, 2017 at 13:29
  • 1
    @TomasZato: yes you can have any statements after the colon. It immediately terminates the block, so it's mainly useful when the block is a small one liner.
    – Lie Ryan
    Commented Jan 27, 2017 at 13:29
19

The colon is not really necessary grammatically, had Python been designed in a different world, it's quite conceivable that the language designer might not decide to require the colon. And indeed languages like Cobra does this.

The main reason why colon is required in python is human readability. To quote from Python FAQ:

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.

As also mentioned in the FAQ, the colon also makes it easier to process python code without fully parsing the language. Any text processor which has a full fledged parser, including the python compiler, can do without the colon had it been not required or if it's made optional when unambiguous.

3
  • 10
    “Notice how the second one is slightly easier to read.” I find the first one easier to read. Less noise.
    – user76284
    Commented Jul 2, 2019 at 4:38
  • I find the first one easier to read, because without the colon it looks like two unrelated statements with inconsistent indentation. Add the colon, and it's suddenly a conditional with an attached block. Commented Oct 7, 2022 at 19:51
  • Readability isn't about reducing symbols. Imagine if the conditional expression is very long and has to be broken into multiple lines, and you parenthesise the if-expression to do so; with the colons, it's easy to see that the conditional expression has not ended because there's a missing colon at the end of the line. If a colon wasn't required, it wouldn't be obvious whether the next line belong to the conditional expression or to the if-block.
    – Lie Ryan
    Commented Oct 8, 2022 at 16:10
13

It is not necessary for the computer, but for humans.

Guido van Rossum (creator of Python) had a Python history blog for a while. The colon was introduced in ABC, the source of many of Python's features.

In this blog post on "Karin Dewar, Indentation and the Colon", Guido writes:

And here I will paraphrase, at Lambert's request.

In 1978, in a design session in a mansion in Jabłonna (Poland), Robert Dewar, Peter King, Jack Schwartz and Lambert were comparing various alternative proposed syntaxes for B, by comparing (buggy) bubble sort implementations written down in each alternative. Since they couldn't agree, Robert Dewar's wife was called from her room and asked for her opinion, like a modern-day Paris asked to compare the beauty of Hera, Athena, and Aphrodite. But after the first version was explained to her, she remarked: "You mean, in the line where it says: 'FOR i ... ', that it has to be done for the lines that follow; not just for that line?!" And here the scientists realized that the misunderstanding would have been avoided if there had been a colon at the end of that line.

(B here is a series of prototype languages B0, B1, ... that led to ABC. It's not the B language that is the predecessor of C).

I also remember Guido pointing out in the 90s that it was for the benefit of editors, that can automatically insert an indent after a line that ends with a colon. But I haven't found a source for that yet.

5

The Cobra Programming Language's syntax is heavily inspired by Python's, and it does away with the colon, so it seems that it is not strictly necessary. However, it is not enough to just remove the colon, there are other changes to the syntax needed. See, for example this piece of code from one of my toy projects:

kons  = lambda hd, tl: lambda x: hd if x else tl
virst = lambda l: l(True )
rrest = lambda l: l(False)

Without the colon to separate the body from the parameter list, I would have to use indentation:

kons  = lambda hd, tl
    lambda x
        hd if x else tl

virst = lambda l
    l(True )

rrest = lambda l
    l(False)

I believe earlier versions of Cobra made the colon optional, you could use either indentation or a colon or both. Similar to how it works in Ruby, where there are keywords to separate the different parts of control expressions, but you can also use expression separators (semicolon or newline):

# idiomatic
while true do puts "I am awesome" end
#          ↑↑

# non-idiomatic, but legal
while true; puts "I am awesome" end
#         ↑

# non-idiomatic, but legal
while true
puts "I am awesome" end

# idiomatic
while true
  puts "I am awesome"
end

In current versions of Cobra, you can use a comma:

if x
    y

can be written as

if x, y

You need some way of separating the different parts of control expressions or definitions. In Python, that is the colon. If you remove the colon, you need to replace it with something else, e.g. forced indentation. Only removing the colon won't work.

The only way to be absolutely sure is to formalize the syntax with and without colon and prove its non-ambiguity.

Note, however, that one of the aphorisms of the Zen of Python is "Explicit is better than Implicit", so the explicit delineation of blocks with colons seems to fit with the general philosophy of Python. The Design and History FAQ also mentions that this decision is based on empirical evidence from Python's predecessor, ABC.

1
  • 4
    Well, with that philosophy described in the last paragraph, you could require colon at the end of every line. Explicit vs implicit only makes sense if explicit actually adds information (eg. the implicit variant is ambiguous). Which was the point of my question. Commented Jan 27, 2017 at 12:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.