On Wed, 22 Jul 2020 at 04:47, Paul Sokolovsky email@example.com wrote:
On Tue, 21 Jul 2020 17:48:35 -0700 Christopher Barker firstname.lastname@example.org wrote:
for something in some_iterable: some_stuff_with_maybe_a_break else if not break: something_more
No new keywords :-)
for something in some_iterable: some_stuff_with_maybe_a_break else: # if not break: something_more
This is the most genius solution posted to this thread.
And if the thread didn't die after van Rossum's post, I don't cheer hopes it would die now, but let me sprinkle in another "fresh" thought. First let me remind how this thread started:
ELSE-clause in FOR and WHILE has unclear syntax. I suggest new clause instead
Then various people started to throw in variants of even more unclear and confusing syntax (like "if" after "for"), with some percentage of those people demonstrating not understanding how the original "else" works.
But consider following idea: if it's confusing, DO NOT USE it. You don't hear that often, but: treat Python as a generic programming language, not as a bag of tricks. The fact that most other languages don't have extra clause after "for" is enough of a reason to not use it in Python either.
The problem isn't that it is unclear, the problem is that it is
misunderstood. If it was unclear what `for...else` means/does then yes, do-not-use would fix the problem. The fact that it can and *is* *mis*understood as doing something else means that people have to learn to not use something that feels like it *should* be usable.
So why it exists at all then? It's there for people who don't find it confusing, for very responsible use. As a random example, after studying 2 (bytecode) Python compilers and having written my own (so I know what code is generated from for-else), I no longer find it confusing. I actually found 2 intuitive ways to use that construct, in the very compiler mentioned - after all, if you eyeball a Python compiler, you either know, or ready to learn, how all language constructs work.
For anything else - clear ban. Everyone should consider that too. (But please leave the language syntax alone (backwards compatibility, etc.), at least that's a suggestion which comes out from van Rossum's post).
Frankly, saying that a part of a language that is frequently misunderstood, is *never* allowed to be improved is disappointing when the suggestion that it can be (somehow) has been so well received by everyone else.
As for the "loop didn't run at all" case: Does anyone find a need for that? Personally, I've found that everytime I do some kind of check for an empty iterable before a loop, it was totally unnecessary.
I find the need for that regularly. And I use the obvious syntax which everyone else uses:
if not seq: print("This page is intentionally left blank") else: for i in seq: ...
Any adhoc, confusing syntax someone may imagine to add, would go to the same "DO NOT USE" category. So, I pray to van Rossum's answer that something like that will never be added to the language. (Which is hard to hang hopes on, given all the mess added recently.)
Many people offering approaches is not muddling the thread. Clearly, most people here agree that `for...else` is confusing/unintuitive on it's own, and could be improved (not removed, but improved). The discussion was trying to find an improvement that people agree on as well. The reason there are so many offerings, is probably because everyone has had a similar thought about it in the past.