An interesting beginner question: why we need colon at all in the python language?

Thorsten Kampe thorsten at thorstenkampe.de
Wed Jul 13 13:26:21 CEST 2011


* Steven D'Aprano (Wed, 13 Jul 2011 21:07:17 +1000)
> Thorsten Kampe wrote:
> > * Thomas Jollans (Mon, 11 Jul 2011 16:16:17 +0200)
> >> Basically, it looks better, and is more readable.
> > 
> > People tend to overlook the colon for the same reason they tend to
> > forget to set the colon in the first place:
> > a) it's a very weak marker in comparison to indentation and
> > b) it looks like doubling the markup to them (colon plus indentation)
> 
> I can't speak for others, but speaking for myself, I wonder whether this is
> a difference between English speakers and non-English speakers?

It's not a difference between English and non-English speakers but the 
difference between a branch (if-then-else) and an enumeration (your 
example).

> To me, as a native English speaker, leaving the colon out of a header
> line, as follows below, just looks wrong.
> [enumeration] 
> 
> Although the bullet list is indented, the header line "Our three weapons
> are" looks like something is missing, as if I had started to write
> something and forgotten to finish. It needs a colon to be complete:

Sure, because it's an enumeration - and not a branch or loop.
 
>     An indented block on its own is surprising. It just hangs there, 
>     with no connection to what was going on before. Why is it indented?
>     Is it connected to the previous sentence?

In normal text: sure. You cannot "just indent" in Python as you like. 
Indentation always shows the connection.
 
> >> A colon, in English like in Python, means that something follows
> >> that is related to what was before the colon. So the colon makes it
> >> abundantly clear to the human reader that a block follows,
> > 
> > The block that follows makes it abundantly clear to the human reader
> > that a block follows.
> 
> But it's too late by then. You have to mentally backtrack.
> 
> blah blah blah blah <statement is complete>
>     indented block <surprise the previous line wasn't complete>
> 
> blah blah blah blah colon <statement is not complete>
>     indented block

Source code is (unlike normal text) not read line by line. So you (at 
least I) don't have to backtrack from line 2 to line 1 because you see 
them both at the same time.

Thorsten



More information about the Python-list mailing list