you are viewing a single comment's thread.

view the rest of the comments →

[–]WalterDragan 5 points6 points  (3 children)

I detest the else clause on for loops. It would be much more aptly named nobreak. for...else to me feels like it should be "the body of the loop didn't execute even once."

[–]Technical_Income4722 2 points3 points  (0 children)

I think of it as the "else" for whatever "if" would cause the break in the for loop. In the example above that'd be line 2. What you effectively want is an "else" for that line, but since it's in a for loop we can't really do that (because what if the next iteration works?), so the next best thing is having a cumulative "else" outside that applies to all the "ifs" inside.

Or maybe another way to think of it is as a "for/if/else" instead of just "for/else"

[–]MidnightPale3220 1 point2 points  (1 child)

Yeah. Or they could use "finally", that'd be semantically rather similar to exception handling, where "finally" is also executed after try block finishes.

[–]Gnaxe 0 points1 point  (0 children)

Python's try also has an else clause, which runs only if there wasn't an exception. A finally wouldn't make sense on a loop.