If someone _is_ going to do this, I have one more suggestion that I didn't raise last time around, because I don't want to argue for it, but for completeness:
Briefly: add if and while clauses to the for statement; redefine comprehensions as nested for loops.
The basic problem is that Python comprehensions map to arbitrarily nested for and if statements, and there's no way to add "while" semantics cleanly with another nested statement.
If you look at the languages that have while clauses, this is not how their comprehensions work.
Some of them, they have nested loops only, but each loop has optional modifiers that Python's don't: an if or when clause that filters, a while clause that terminates early, maybe unless and until clauses that do the same but with negated conditions (although until has a choice between doing the last iteration or not doing it). This allows for everything you can do in Python except multiple if clauses on the same for clause (which are usually better rewritten with and--or, when that's too unwieldy, you probably shouldn't have been writing it as a one-liner).
In others, there's a single loop, but it takes nested iterable expressions, so you only get one set of modifiers for all of the iterables.
Others just don't have any syntax for nested iteration at all; you have to turn it upside-down and use the outer loop as the iterable for the inner loop.
This would have another minor arguable benefit. Every once in a while I see some code like this:
for x in (x for x in xs if f(x)):
... because the writer didn't think long enough to realize he doesn't need the comprehension. This code is obviously a lot better as:
for x in xs if f(x):
But that example shows exactly why I don't think this is worth arguing for. The same code is even better as:
for x in xs: if f(x):
And that brings us back to the reason Clojure, Racket, etc. have clauses on their for loops: Because they don't have loop statements, they have a loop function. (Well, usually it's a macro that quotes the arguments so you don't have to lambda all your expressions into functions, and to allow you to avoid at least one excess repetition, but let's ignore that.) The when and while clauses are just keyword arguments to the function. More generally, they encourage or require you to write everything as a complex expression, which is the exact opposite of what Python encourages.
Still, just because the idea comes from languages that are very different from Python doesn't necessarily mean it wouldn't fit; after all, that's how we got comprehensions in the first place.
Sent from a random iPhone
On Nov 21, 2014, at 8:36, Ethan Furman email@example.com wrote:
On 11/21/2014 08:22 AM, Andrew Barnert wrote:
Maybe what we need to do is update PEP 3142, gathering all of the alternative ideas, and expanding on the rationale, so Guido can reject that, and next time this comes up, everyone will have something to read before having the same arguments about the same proposals?
Are we accepting nominations? I have the perfect fellow in mind. :)
(And in case Chris decides to nominate me in retaliation, I decline. ;)
Python-ideas mailing list Pythonfirstname.lastname@example.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/