19

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?

  • 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. – Tomáš Zato Jan 27 '17 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 Jan 27 '17 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? – Tomáš Zato Jan 27 '17 at 12:03
  • Got it! It's a question around python's syntax design decisions. Sorry, misunderstood it. Thank you for explaining. – bhan sur Jan 27 '17 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 Jan 27 '17 at 12:23
9

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 langauge 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.

  • 1
    Wait, you can have a statement after the colon on the same line? I was pretty sure it's not allowed. – Tomáš Zato Jan 27 '17 at 12:00
  • 1
    It is permitted, but only with a linebreak after it. – Phoshi Jan 27 '17 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. – Tomáš Zato Jan 27 '17 at 12:17
  • Sure, just try it. – RemcoGerlich Jan 27 '17 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 Jan 27 '17 at 13:29
14

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
    “Notice how the second one is slightly easier to read.” I find the first one easier to read. Less noise. – user76284 Jul 2 at 4:38
10

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.

4

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.

  • 3
    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. – Tomáš Zato Jan 27 '17 at 12:01

protected by gnat Sep 5 '17 at 13:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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