An interesting beginner question: why we need colon at all in the python language?
steve+comp.lang.python at pearwood.info
Wed Jul 13 07:07:17 EDT 2011
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? To me, as a
native English speaker, leaving the colon out of a header line, as follows
below, just looks wrong.
Nobody expects the Spanish Inquisition! <OMINOUS MUSIC>
Our three weapons are
* and ruthless efficiency
* and an almost fanatical devotion to the Pope!
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:
Nobody expects the Spanish Inquisition! <JARRING CHORDS>
Amongst our weapons are:
* ruthless efficiency
* an almost fanatical devotion to the Pope
* and nice red uniforms
The colon indicates that the sentence has more to follow: I think of it as a
pointer. It doesn't finish the thought, like a full stop, nor is it a mere
pause, like a comma or semi-colon.
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?
On the other hand, a colon gives the reader that connection:
It gives the reader a clue to expect additional information,
that the indented block that follows is not an independent
block, floating in space for its own reasons, but is intimately
linked to the previous line.
I know that Python is not English, and not all English conventions apply.
For example, Python uses newlines to end "sentences" (lines of code)
instead of a full stop. Nevertheless, as an English reader, it would look
wrong and awkward to drop the colon.
>> 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>
More information about the Python-list