3 ms·
I'm a strong python advocate and rarely use for/else for this exact reason. It makes more sense to me for it to be the other way (your way) around. I always add
by rlayton2 8y ago
I'm a strong python advocate and rarely use for/else for this exact reason. It makes more sense to me for it to be the other way (your way) around. I always add a comment on the else when I do too, to ensure that future readers don't make the same mistake.
- deleted 8y ago[deleted]
- bjd2385 8y agoI agree. It's seldom something I've used in my scripts/small projects for this exact reason. It should also be noted that there is a `while-else` compound statement as well, wherein using a `break` immediately exits the loop, without executing the `else` clause [1]. [1] https://docs.python.org/3/reference/compound_stmts.html#the-while-statement https://docs.python.org/3/reference/compound_stmts.html#the-...
- kchr 8y agoYeah, it really feels (almost in a physical sense) like an anti-pattern... I have to force my fingers to write it.
- joelcornett 8y agoI’ve never found a compelling reason to use this construct (or the analogous `while...else` construct). But then again, I rarely find myself using `else` with `if` either...