Loop with else clause
The topic is how to deal with a python loop that may not be run if you want
something else to happen in that case. Multiple solutions are presented
along with this request:
> Is there another, more pythonic, approach to conditional (for/while)
> loop processing?
Has anyone considered looking at this from the perspective of the CONDITION
If the condition is something simple like is the "list" empty, then any
method will work as it can be evaluated before trying the loop with no side
But what if the item in question is complex and constructed dynamically as
by zipping things together or calling functions and might even produce side
effects? It might be far from trivial to do that before the loop and check
and you might inadvertently change things. Using a sentinel approach might
And what about loops that start but exit immediately without doing anything?
If you iterate on something whose first value is wrong in some way and the
loop starts with an "if ... break" then do you want to do the ELSE condition
because the loop sort of did not run as anticipated? A similar example is if
the loop starts with a "try" and the error handler does a break.