
On Thu, 3 Mar 2022 at 19:54, Steven D'Aprano <steve@pearwood.info> wrote:
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 has a history of making conceptual actions shorter than the mere combination of their parts. For instance, we don't have this construct: for i in range(len(stuff)) using thing = stuff[i]: No, we have this: for i, thing in enumerate(stuff): No matter how much you personally pooh-pooh the idea, filtered iteration is a very real concept, and people do it as best they can with the idioms available. In Python, that usually means putting part of the iteration code into the body of the loop: for thing in stuff: if not isinteresting(thing): continue There's an inefficient and verbose option of the genexp, but I don't think many people like it: for thing in (thing for thing in stuff if isinteresting(thing)): although when it's actually a function like this, you have this option: for thing in filter(isinteresting, stuff): which actually looks good. I think this is a pretty clear indication that the idea makes sense: functional programming languages have an idiom that aligns perfectly with it, it's just that Python's lambda syntax is too clunky for inline expressions to look as good. for thing in filter(lambda n: n % 7 < 2, stuff): I do not think Python should have a syntax which is merely removing a newline from what can already be done. But a proper syntax for filtered iteration WOULD be of extreme value. In bracey languages, I'll often combine the 'if' and 'for' onto one line, because newline flexibility lets me write filtered iteration that way. It's not ideal but it's a lot better than nothing: for (....) if (...) { ... } What Python needs is not a way to cram more onto one line. What Python needs is a way to express an abstract concept: "iterate over the interesting parts of this collection".
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.
You're thinking FAR FAR too concretely about this. It's not about newlines. It's about expressing programmer concepts.
Fresh strawberries are great. Mushroom sauce is great. But strawberries with mushroom sauce is ... not.
You DO know that you just made several people think "hmm, maybe I should try strawberries with mushroom sauce", right? ChrisA