Whitespace indentation to delimit code blocks gives much more than just getting rid of small syntactic
sugarnoise
[in the form of begin/end keywords or curly brackets]. But many programmers still do not get it.
Consider this sample from Nemerle programming language documentation:
https://github.com/rsdn/nemerle/wiki/...:
In Nemerle the if expression always need to have the else clause. It's done this way to avoid stupid bugs with dangling-else:
if (foo)
if (bar)
m1 ();
else
m2 ();
If you do not want the else
clause, use when
expression, as seen in the example.
Most likely, the developers of Nemerle just
took this behaviour from Haskell or Standard ML without understanding the reason of that "stupid bug with dangling-else". But reason is not wrong
if
[allowing an absence of else
clause], and even is not forgotten curly brackets. The reason is divergence in perception of this code by human and with compiler. Human perceives block boundaries via indentation, and compiler via
[subtle for human] symbols. That is what
the root of the problem is a compiler and a human see [this] code differently.
And the solution to this problem is delimiting code blocks via whitespace indentation.
http://compiler.su/dvukhmernyj-sintaksis-python.php:
выражения for операторы цикла :
, if условное выражение :
, else :
заканчиваются двоеточием, без которого можно обойтись.
(statements for ... :
, if condition :
, else :
ends with a colon, which is not very necessary.)
I agree.
Essentially, this colon in Python has no purpose because there are statements (if, for/while, def, class, etc.), which always require a new scope block, and a new line with a larger indentation level is quite sufficient.
By the way, the history of the appearance of this colon is quite interesting.
http://python-history.blogspot.com/2011/07/...:
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... Since they couldn't agree, Robert Dewar's wife was called from her room and asked for her opinion... 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.
That is, scientists, the developers of a programming language, heeded the words of someone who has no relation to programming. Did they want to simplify learning language for novices? But a programmer remains a novice only a short time at the beginning of his career, but observing and typing excess colons will have to continue.
The only useful application of colon is one-line if's (
if condition: statement
) and loops, but a good alternative to this is additional curly brackets around a statement (
if condition {statement}
, like in Rust) or parens around a condition (
if (condition) statement
).