The easiest way I found to 'get' what the for/else did, and more importantly, when to use it, was to concentrate on where the break statement jumps to. The For/else construct is a single block. The break jumps out of the block, and so jumps 'over' the else clause. If the contents of the else clause simply followed the for clause, it would never be jumped over, and so the equivalent logic would have to be provided by putting it in an if. This has been said before, but not quite in these words, so it may help somebody else. Try running the following code fragment. I'm wholeheartedly in favour of the 'no break' comment for clarity.
for a in range(3):
print(a)
if a==4: # change value to force break or not
break
else: #no break +10 for whoever thought of this decoration
print('for completed OK')
print('statement after for loop')
EDIT - I notice this question is still running
Second better thoughts ...
The 'no break' comment is a negative. It's so much easier to understand a positive assertion, and that is that the for
iterable was exhausted.
for a in range(3):
print(a)
if a==4: # change value to force break or not
print('ending for loop with a break')
break
else: # when iterable exhausted
print('ending for loop as iterable exhausted')
print('for loop ended one way or another')
That also reinforces this interpretation
if iterable_supplies_a_value:
run_the_for_with_that_value
else:
do_something_else
A place I've been using it recently is with a for
instead of a while True:
loop, when early debugging some recursive or search function. There's nothing more annoying than a silly error in the finish loop condition hanging the program, so I tend to use for _ in range(MAX_ITERATIONS):
instead, which leads to the following use ...
for _ in range(MAX_ITERATIONS):
do_stuff()
if end_condition:
break
else:
log('doh, messed up that break condition again!')
log('some maybe helpful debugging data')