3 ms·
I'm partial to for-else-loops. They fit perfectly into languages with compound expression (produce a value from a break in the loop or from the else block). Th
by atq2119 4y ago
I'm partial to for-else-loops. They fit perfectly into languages with compound expression (produce a value from a break in the loop or from the else block).
They don't come up that often, but when they do they're really the best solution.
- skykooler 4y agoThey also seem to be unknown to a lot of Python programmers unfortunately; I’ve had PRs rejected because a for-else loop was “unusual syntax” and therefore considered hard to maintain.
- thiht 4y agoI used one recently in a script and they're definitely not that readable. It always takes some time what "else" means in the context of a loop. I feel like it would work better with a more explicit keyword, but I don't know which one. `nobreak`?
- cpurdy 4y agoI hate the Python syntax using "else" for this, but I love the feature. I've often wanted both a "then" and an "else" from both for and while loops. The "then" would be for a successful completion (no break), and the "else" would be for when the loop doesn't even run a single iteration. But that didn't make it into our "language budget", unfortunately. It's easy to implement, but hard to argue for when it doesn't get used often