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

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

Example:

if n == 0: print "The end" 
+64
python syntax
Oct 18 '08 at 21:18
source share
5 answers

The colon should announce the beginning of the backing block.

Technically, this is not necessary; you can just backtrack and back down when the block is done. However, based on the Python koan , “explicit is better than implicit” (EIBTI), I believe Guido deliberately made the colon mandatory, so any statement that should be followed by indentation of the end of the code with a colon. (It also allows single-line if you continue after the colon, but this style is not used widely.)

It also makes it easier to work with syntactic autoslip editors, which is also taken into account in the solution.




This question turns out to be a Python FAQ , and I found one of its Guido answers here :

Why are colons needed for if / while / def / class statements?

A colon is primarily required to increase readability (one of the results of an experimental ABC language). Consider this:

 if a == b print a 

against

 if a == b: print a 

Pay attention to how the second is easier to read. Also notice how the colon gives an example in this answer to frequently asked questions; its standard use is in English.

Another minor reason is that the colon makes syntax highlighting easier for editors; they can look for colons to decide whether to increase the indentation, rather than perform a more thorough analysis of the program text.

+71
Oct 18 '08 at 22:49
source share

Consider the following list of things you can buy at a grocery store written in Pewprikanese.

 pewkah lalala chunkykachoo pewpewpew skunkybacon 

When I read this, I am confused: "Are chunkykachoo and pewpewpew some kind of lalala? Or what if chunkykachoo and pewpewpew backtrack only because they are special items?

Now let's see what happens when my friend Pewprikanese adds 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 begins to learn Python, which turns out to be her first programming language to learn. Without a colon, there is a significant chance that she will continue to think: "These lines are indented because these lines look like special elements." And it may take some time to realize that this is not the best way to think about retreating.

+21
Sep 23 '09 at 8:13
source share

Three reasons:

  • Improve readability. A colon helps you convert code to the next pending block.
  • To help text editors / IDEs, they can automatically indent the next line if the previous line ends with a colon.
  • Making parsing in python a little easier.
+15
Oct 19 '08 at 5:40
source share

As far as I know, this is an intentional design to make it more obvious so that the reader can expect indentation after the colon.

He also makes such constructions as possible:

 if expression: action() code_continues() 

Please note (as the commentator noted) that this is not a completely brilliant gold standard of good Python style. It would be much better to have a space:

 if expression: action() code_continues() 

to avoid confusion. I just wanted to make it clear in the first example that it could be written like this, since having the code for if right after the colon allows the compiler to understand that the next line should not be indented.

+5
Jun 19 '10 at 12:00
source share

According to Guido Van Rossum, the inventor of Python, the idea of ​​using a colon to make the structure more obvious is inspired by earlier experiments with Python's predecessor, the ABC language, which also aimed at newbies. Apparently, in the early tests, novice students progressed faster with a colon than without them. Read the full story on the python post history blog in Guido.

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

And yes, the colon is useful in single-line and less annoying than the semicolon. It is also recommended that you style a long time style guide into several lines only when it ends with a binary operator.

 x = (23 + 24 + 33) 

Adding a combined statement with the colon looks the same for more uniform style.

There is an "endless" encoding for CPython, as well as a colon without a dialect, called cobra. They did not pick up.

0
Sep 21 '18 at 21:06
source share



All Articles