3 ms·
https://twitpic.com/4a52sh https://twitpic.com/4a52sh A survey on this feature was done at Pycon 2011. Maybe things have changed since then, but at the time, m
by nickm12 8y ago
https://twitpic.com/4a52sh https://twitpic.com/4a52sh
A survey on this feature was done at Pycon 2011. Maybe things have changed since then, but at the time, most people didn't understand what for/else did.
I think it's a bad construct because, as many have mentioned, a lot of people make the commonsense interpretation that the else block will only execute if the for block did not.
Note, the link above come via Jeremy Avnet in the comments of this blog post.
https://nedbatchelder.com/blog/201110/forelse.html https://nedbatchelder.com/blog/201110/forelse.html
- mar77i 8y agoPersonally, I'm really struggling with the idea which is underlying to this feature, and I find it relieving to find this article and survey result. - Does the else realize that the loop contains an if block? - How does a break statement relate to the StopIteration exception? - How does the else block run if the range wasn't, technically, empty?
- rplnt 8y ago> the commonsense interpretation that the else block will only execute if the for block did not That is exactly what it does. So when you explicitly break, the for body was executed. As for the poll, the first answer is not wrong. It's first option even, the question is vague enough. I wouldn't read that much into it, 33+24+9 (66%) people gave correct answer.
- irishsultan 8y agoExcept it isn't what it does. What that option means is that either the loop is entered, or else if the condition is false from the start it will execute the else block.
- rplnt 8y agoIt is what it does. It's not all it does, but the option is correct. Only when looking at other options you would notice there is one that is "more" correct. It's not a poll you can draw any conclusions from. That being said, if you look around, pretty much everyone agrees it is a confusingly implemented feature. I don't think there's any question about that. But at the same time you can find explanations why it is named the way it is, and that can help to remember it better.
- wodenokoto 8y ago> So when you explicitly break, the for body was executed. Maybe that is what is technically happening, I honestly don't know what that means. I consider the body of a for statement to be execute for every loop, and for the loop to normally end when all values have been iterated. And else-clause is something that happens instead of the intended clause, which makes it very hard to fit in. Reading through the comments here, there does seem to exist a mental model where the else makes sense. Something along the lines of `if` there still is a value to assign to `i` do `loop-body`, `else` do `else-body`. You can learn that, and it does make sense, but it is not the mental model the majority of people will build when they see a normal for loop in Python.
- nickm12 8y agoI think you're mistaken on both counts. The first answer is what I call the "commonsense interpretation" and it is incorrect about what python's for/else does.
- rplnt 8y agoI don't know what to tell you... For break to be called explicitly it has to enter the loop body - there is no other way. The break only works in the loop body. And the first answer is correct in the sense that it is not wrong (try it). The poll is simply flawed.