3 ms·
I agree that the word 'else' is confusing here. To me, it suggests that one thing is done, or the other, but not both. Python's try..except..else follows this p
by zephyrfalcon 8y ago
I agree that the word 'else' is confusing here. To me, it suggests that one thing is done, or the other, but not both. Python's try..except..else follows this pattern, but for..else and while..else do not, for some reason.
- ehsankia 8y agoEvery time I go to use for...else, I need to look up what the behavior is, I can never remember if it's hitting the break or not hitting the break. That being said, python always tries to minimize the number of keywords, so re-using else makes more sense then introducing a whole new keyword for a niche usecase.
- fjsolwmv 8y ago...and then you realize that's why you shouldn't use it, right?
- fyi1183 8y agoThe way I remember it is that it makes sense in loops that search for something in an iterable. Such loops break when they're successful, and so it makes sense for the code in the else clause to run when the loop was unsuccessful.