开发者

What is the benefit(s) of having 'else clause' for the while loop in python?

Any code after while loop will execute when the condition in the while loop becomes False. I开发者_JS百科t is the same for the code in the 'else clause' section of while loop in python. So What's the advantage of having 'else' in the while loop?


else will not execute if there is a break statement in the loop. From the docs:

The while statement is used for repeated execution as long as an expression is true:

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

(emphasis mine) This also works for forloops, by the way. It's not often useful, but usually very elegant when it is.


I believe the standard use case is when you are searching through a container to find a value:

for element in container:
    if cond(element):
        break
else:
    # no such element

Notice also that after the loop, element will be defined in the global scope, which is convenient.


I found it counterintuitive until I heard a good explanation from some mailing list:

else suites always execute when a condition has been evaluated to False

So if the condition of a while loop is executed and found false, the loop will stop and the else suite will run. break is different because it exits the loop without testing the condition.


The else clauses for the looping constructs was to eliminate flags to distinguish between normal and "abnormal" loop exits. For example, in C you might have:

int found = 0;
for(int i = 0; i < BUFSIZ; i++) {
    if(...predicate..) {
       found++;
       break;
    }
}
if(found) {
    // I broke out of the for
} else {
    // the for loop hit BUFSIZ
}

Whereas with a loop-else you can eliminate the (somewhat contrived) found flag


quoting ars: "The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed."

See Else clause on Python while statement .


The else suite on Python loops is best thought of for the case where the loop is performing a search. It's where you handle the case where your search was unsuccessful. (There may be other cases where you might use this, but this is the most common and easily remembered user/case).

The alternative would be to use a sentinel value:

sentinel = object()
result = sentinel
for each_item in some_container:
    if matches_some_criteria(each_item):
        result = each_item
        break
if result is sentinel:
    do_something_about_failure()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜