On Thu, Mar 03, 2022 at 02:32:25AM +0000, Rob Cliffe via Python-ideas wrote:
> But the proposal would give people the choice of
> Saving a level of indentation at the cost of having two
> suite-introductions on the same line.
> Keeping the two suit-introductions on separate lines (as now) at
> the cost of an extra level of indentation.
Why only two? Why not more than two?
for item in seq: if cond: while flag: with something as x: for y in
x.thing(): if condition:
block
Many other languages allow developers to cram code into enormous
one-liners. Should we do the same?
This is not a rhetorical question. And I have a non-rhetorical answer.
In my opinion, no we should not.
Python, for good or ill, is an opinionated language regarding
indentation and statements. The general rule for compound statements is
that the statement that introduces the block must appear on its own
line. While you can put the block on the same line as the colon using
semicolons:
if condition: print(1); print(2) # Legal.
you can't introduce a new block:
if condition: for x in loop: # Not legal.
A compound statement that ends with a colon is either either followed by
a newline and one indent, or a series of semicolon separated simple
statements. Never by another compound statement.
This is, I believe, partly to keep the parser simple, and partly to keep
the code easy to read, write and reason about.
Could we do something different? Of course we could. But would the
result still feel like Python? I don't think so.
Ultimately, the choice comes down to taste. Python is not Perl, or APL,
or Forth, or Hyperscript, or Inform. Anyone who has been reading my
emails over the years knows that I am particularly fond of Forth and
Hyperscript, and yet I would not want Python to be more like either of
them. You don't have to think that a syntactic feature is *bad* to think
that it doesn't belong in Python.
Fresh strawberries are great. Mushroom sauce is great. But strawberries
with mushroom sauce is ... not.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/VHVDFWLVTH76WZZ4UXAA4QDGACDGDB4R/
Code of Conduct: http://python.org/psf/codeofconduct/