2 ms·
> It is a legitimate alternate clause in the sense that it provides alternative behavior when the guard fails. But the "guard fails" and the else is executed o
by acqq 8y ago
> It is a legitimate alternate clause in the sense that it provides alternative behavior when the guard fails.
But the "guard fails" and the else is executed on every normal loop exit, except if the exit was done with break.
for a in range( 1,3 ):
print( a )
else:
print( "end" )
gives:
1
2
end
It's an "alternative" only when there's break. In all other cases it is executed.
Even naming it "nobreak" would be confusing as without break it is always executed at the end. The clearer naming would be approximately "on_every_nonbreak_end_of_for"