6
$\begingroup$

I came across this question on Stack Overflow and it got me wondering... why did Python choose to allow single line compound statements? Python has significant whitespace, and it seems like the parser could easily differentiate structure based on newlines and indentation levels.

Looking at the Python grammar it appears that anything that has the structure KEYWORD named_expression ':' block uses the colon to delineate the end of the expression and the start of the following block, presumably so that parser can tell which is which when the statement is crammed on to one line but that has been considered "bad style" and all but unused since before I started programming well over a decade ago. Is this just a vestigial remnant of the early days? If so why wasn't it dropped in the 2/3 rewrite? Is there some benefit or grammatical complication that I'm missing here that makes this necessary?

For example:

# current Python syntactic options
if True: print('hello world')
if True:
    print('hello world')

# vs a hypothetical Python that disallows the first form above
# and instead uses NEWLINE/INDENT to delimit the expression vs 
# the following block:
if True
    print('hello world')
```
$\endgroup$
19
  • 1
    $\begingroup$ To be clear, by "single-line compound statements" you mean like if x: f() without a newline, not like f(); g() with a semicolon? $\endgroup$
    – kaya3
    Jan 2 at 15:09
  • 5
    $\begingroup$ Not necessarily the motivation for the language itself, but single-line programs are very handy when using Python in place of awk and friends in the shell. $\endgroup$ Jan 2 at 17:11
  • 3
    $\begingroup$ Note: the colon has been motivated in an old blog post by GvR as punctuation aimed at humans, not the parser. It exists to visually denote that the following belongs to the preceding statement, just like a colon in prose would indicate a list of examples belongs to its preceding sentence naming the category. Since Python generally has no a b expression syntax, there is little or even no ambiguity in omitting the colon. $\endgroup$ Jan 2 at 17:14
  • 2
    $\begingroup$ Previously asked on SO: stackoverflow.com/questions/61186871/… $\endgroup$
    – dan04
    Jan 2 at 18:09
  • 2
    $\begingroup$ Also: softwareengineering.stackexchange.com/questions/104066/… $\endgroup$
    – dan04
    Jan 2 at 18:09

2 Answers 2

1
$\begingroup$

Compound statements are produced by the parser as part of the "suite" production, which groups up nested scopes into a syntax tree. One-line compound statements are allowed by not requiring an "INDENT" token. The linked documentation states that one-line compound statements are as legal as multi-line compound statements.

The only recommendation against one-line compound statements is in PEP 8, which is an optional guide that does not apply to all codebases. One-line compound statements save vertical space and overall whitespace, and this is the main attraction towards including them as a feature in non-Python grammars.

$\endgroup$
4
  • 1
    $\begingroup$ Let me know if you want more words. I think that the points about style are important to the history of Python and the Python ecosystem, but almost totally irrelevant to PLT/PLDI; if you want your grammar to accommodate one-liners, then something like this is worth considering. $\endgroup$
    – Corbin
    Jan 3 at 17:01
  • $\begingroup$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review $\endgroup$ Jan 4 at 9:44
  • $\begingroup$ @nchistov: How about now? Note that the original question is three questions, with the latter two begging the first. It happens to be the case that the first question's answer is a resounding "no, one-liners are completely allowed in Python, and only style guides say otherwise." $\endgroup$
    – Corbin
    Jan 4 at 20:19
  • $\begingroup$ Now I think is ok. $\endgroup$ Jan 5 at 11:04
1
$\begingroup$

Mostly to make it easy to write one-liners like python -c "for i in range(0,10): print(i)"

$\endgroup$
1
  • 7
    $\begingroup$ Do you have any citations that support this as the main motivation? $\endgroup$ Jan 4 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .