4 ms·
> So it is very analogous to an if / else statement: the “else” after a for or while loop directly means the conditional of the loop evaluated to False. ...how
by thaumasiotes 8y ago
> So it is very analogous to an if / else statement: the “else” after a for or while loop directly means the conditional of the loop evaluated to False.
...how is that supposed to apply to "for i in l"? What is the conditional?
I don't think of python for statements as involving conditionals at all; you seem to be describing a while/else rather than a for/else.
- kqr 8y agoThink along the lines of "for (l = lst.iterate(); l != null; l.next())".
- mlthoughts2018 8y agoThis is right; it’s the StopIteration exception (which is fundamental to what a for loop does in Python). Not saying is absolves the for/while else syntax, but it’s still using the expected iteration constructs.
- thaumasiotes 8y agoWouldn't that imply that the equivalent of a while/else loop would be a for/except loop, not a for/else loop?
- mlthoughts2018 8y agoAs a matter of trivia, probably not. The iteration mechanism just happens to check for StopIteration, and does not automatically handle any other type of Exception, which would require a usual try block. So StopIteration can be thought of as an equivalent idea to “emptiness” of an iterator, either an empty container, an exhausted generator, etc. And since emptiness is most often handled as falsiness in a bool context, it’s in some sense the natural notion of a “false” iteration condition. This is all incredible mental gymnastics though. But (speaking as someone who loves Python) there’s unfortunately a lot of that in Python.
- fjsolwmv 8y agoThat's deeply "unpythonic"
- tzs 8y ago> ...how is that supposed to apply to "for i in l"? What is the conditional? Presumably there is a conditional in the loop body that decides whether or not to break: for i in l: if ...: break Think of the else clause as being what to do if nothing takes the if in the body. Same for while...else. You'd only be using an else on a while if there was something in the while that could break, so it is whether or not the conditional that controls that decision happens or not that determines whether the else path is taken.
- thaumasiotes 8y agoHuh? Judging by every other comment in the thread, the point of for/else is that breaking does not trigger the else clause. The else clause is triggered when the loop condition is tested and fails... but Python's for doesn't have a loop condition.
- gh02t 8y agoNo, an explicit break skips the else clause and the else clause is evaluated if and only if the iterator in the loop raises a StopIteration exception (i.e., it's exhausted). For example, this code will still evaluate the else clause even though the break statement is always true, because iter([]).next() throws StopIteration immediately: for i in []: if True: break else: print("Foo") while this will not: for i in [1] if True: break else: print("Bar") The "conditional" in a for loop is that the iterator (l in your example) raises a StopIteration exception. I don't really like calling it a conditional though, because it's actually handled as an exception. I guess you could think of the condition being "are there any items remaining in l? If 'no' then evaluate the else clause." I've always considered the else clause on a for loop's proper purpose being to catch erroneous cases, for instance if you try and loop over files in an empty directory. Or if you have no matches searching line by line over a file. Etc.
- noobermin 8y agoYou're right, technically, but the way tzs thinks about it can still be correct if one thinks about it this way: the if statement in your first loop is mathematically true, but it is never executed because [] is of course empty. It's not about whether the inner if ...: break statement's conditional can evaluate to true. A lazy way to think about it is that way, but as long as you remember that it's what occurs when there is no break it's fine.