| Someone wrote in |
Why I put 'else' on loops in Python
The real reason is much simpler: I noticed that the bytecode for was false. Then I started looking for use cases, and found a fairly good one: a search loop with special handling if no hits are found. So I decided to add it, and for symmetry I also added it to "for" (where the same use case exists).
The real reason is much simpler: I noticed that the bytecode for
while TEST:
BLOCK
was very similar for that of
if TEST:
BLOCK
(Except that the while block ended with a jump to the top.) Then I realized that the "if" version had an optional "else" clause and started thinking about what that would mean for "while". Obviously it would be something that would be executed when That's the whole story.
--Guido van Rossum