Someone wrote in [info]leonardo_m,
Why I put 'else' on loops in Python
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 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).

That's the whole story.

--Guido van Rossum



Advertisement


(Read 1 comment)

Post a comment in response:

From:
Help
Identity URL: 
Username:
Password:
Don't have an account? Create one now.
Subject:
No HTML allowed in subject
   Help
Message:
 
Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…