Open selfboot opened 7 years ago
The 'else' clause executes when the loop completes normally. Which means the loop didn't encounter any breaks. Hopefully this makes more sense. So the for loop executes its code then it moves onto the else statement then repeats. If the for loop encounters any breaks the else clause will not execute.
In function
daemon.py/daemon_stop
, we can see:The
for...else
andwhile...else
syntax is one of the most under-used and misunderstood syntactic features in Python, this is a strange construct even to seasoned Python coders.Python doc says:
The primary use-case of for...else is to implement searching:
If there is no
break
in the loop, theelse
clause is functionally redundant. That is, the following two code snippets behave identically:and:
Ref
break and continue Statements, and else Clauses on Loops
[Python-ideas] Summary of for...else threads
Why does python use 'else' after for and while loops?