On Thu, Mar 03, 2022 at 08:04:57PM +1100, Chris Angelico wrote:
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]:
Rather than inventing new (fake) syntax to illustrate your point, a better example would be: for i in range(len(stuff)): thing = stuff[i] which is real code that people have written (including me!), and two statements. Just like the for... if... compound statement being debated now. And we did take that two-line idiom from Python 1.x and turn it into a one-line *functional* style in Python 2, using enumerate().
No, we have this:
for i, thing in enumerate(stuff):
Indeed. What we *didn't* do is invent new syntax for i in range(len(stuff)) using stuff[i] as thing we just came up with a function, enumerate(). If only Python had a function that would allow us to combine iteration and filtering... *wink*
No matter how much you personally pooh-pooh the idea, filtered iteration is a very real concept,
o_O What did I say that made you think I denied the existence of filtered iteration? Was it the post where I pointed out we've been able to do filtered iteration going back to Python 1.x days? To be clear, there are lots of concepts in coding. Not all of them require their own specialised syntax. We don't have specialised syntax for a try...except block inside a loop, we use composition by putting a try...except block inside a for loop. Composition of statements is not a bug to be fixed. [...]
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)):
Indeed. But if you combine map() and filter() with the iteration, it becomes very powerful: for thing in (expression for x in stuff if isinteresting(x)): And with the walrus operator, you can filter on the mapped value as well: for thing in (y:=expression for x in stuff if isinteresting(x or y)):
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.
Nobody said that the idea of filtered looping doesn't make sense. They're only questioning whether it needs its own syntax instead of composing existing syntax.
for thing in filter(lambda n: n % 7 < 2, stuff):
Looks fine to me. But you could also use: for thing in (n for n in stuff if n%7 < 2): which also looks fine to me.
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.
We have proper syntax: compose the for loop with the filter. Or use the functional idiom with filter(func, items). Or various other options such as generators. These are all "proper" syntax. They're just not *dedicated specialist syntax* for filtered iteration. With so many options to choose from, I don't think we need dedicated syntax for this operation. What does it add, aside from bloat?
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".
Something like composition of a for-loop and an if, or filter(). Yes, if only we were capable of doing filtered iteration in Python! Then at last Python would be Turing Complete and a Real Programming Language!!! *wink*
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.
Of course it is. The whole point of the proposal is to move a two line statement into a single line. Earlier in this thread, I pointed out that this proposal adds no new functionality to Python. It doesn't allow us to do anything we can't already do, or even make it easier to do it. Literally all it saves is a newline and an indent.
It's about expressing programmer concepts.
Right. And composing a for-loop with a if statement expresses that concept perfectly. As does filter().
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?
:-) -- Steve