Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspiration and influence of the else clause of loop statements?

Python loop statements may have an else clause which is executed if and only if the loop is not terminated by a break. In other words, when the condition becomes False (with while) or when the iterator is exhausted (with for).

Does this loop-else construct originate from another language (either theoretical or actually implemented)? Has it been taken up in any newer language?

Maybe I should ask the former of Guido, but surely he is too busy for such a futile inquiry. ;-)

Related discussion and examples: Pythonic ways to use ‘else’ in a for loop

like image 394
Aristide Avatar asked May 27 '10 20:05

Aristide


People also ask

What is the role of Else statement in the while loop?

The else part is executed if the condition in the while loop evaluates to False . The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.

What is the purpose of ELSE clause?

The else clause is the inline clause for the if statement to execute when it evaluates to false. Expression to be tested. If it evaluates to nonzero, it is considered true. If it evaluates to 0, it is false.

Do loop statements have else clauses?

21.1.for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement. They are really useful once you understand where to use them.

What are the types of ELSE clause in Python?

The two types of Python else clauses are : i else in an if statement ii else in a loop statement The else clause of an if statement is executed when the condition of the if statement results in false.


1 Answers

A similar feature is found in Common Lisp's LOOP macro, described here by Peter Seibel:

...LOOP provides two keywords, initially and finally, that introduce code to be run outside the loop's main body.

After the initially or finally, these clauses consist of all the Lisp forms up to the start of the next loop clause or the end of the loop. All the initially forms are combined into a single prologue, which runs once, immediately after all the local loop variables are initialized and before the body of the loop. The finally forms are similarly combined into a epilogue to be run after the last iteration of the loop body. Both the prologue and epilogue code can refer to local loop variables.

The prologue is always run, even if the loop body iterates zero times. The loop can return without running the epilogue if any of the following happens:

  • A return clause executes.
  • RETURN , RETURN-FROM, or another transfer of control construct is called from within a Lisp form within the body...

For example, part of a Python sample found in the linked question:

for v in known_variables:
    if self.bindings[v] is cell:
        return v
else:
    raise CannotSimplify

might look something like this:

(loop for v in known-variables
  when (eq (gethash v (slot-value self bindings)) cell)
  do (return v)
  finally (signal cannot-simplify))

Another observation:

Common Lisp's condition system is also unique. Someone, once, asked where it came from and was pointed to Kent Pitman's paper, where he says got it from Maclisp. Similarly, Common Lisp's weird-looking FORMAT function apparently came from Multics via Dan Weinreb.

The common thread is that language features don't tend to follow from the ancestor language that most inspired this language, but are taken by individuals who loved them to whatever new language they're working on. So if you want to find out the actual source of Python's for-else, I'd look for who added it, and see what language they worked on prior to that.

like image 59
Ken Avatar answered Oct 04 '22 14:10

Ken