[Python-ideas] Nudging beginners towards a more accurate mental model for loop else clauses
ncoghlan at gmail.com
Sat Jun 9 02:24:30 CEST 2012
On Jun 9, 2012 10:16 AM, "Terry Reedy" <tjreedy at udel.edu> wrote:
> On 6/8/2012 6:34 PM, Yuval Greenfield wrote:
>> > Loop statements may have an else clause; it is executed immediately
>> after the loop but is skipped if the loop was terminated by a break
> As I said in my reply on pydev, that is misleading. The else clause
executes if and when the loop condition is false. Period. Simple rule.
> It will not execute if the loop is exited by break OR if the loop is
exited by return OR if the loop is exited by raise OR if the loop never
exits. (OR is the loop is aborted by external factors.) As far as else is
concerned, there is nothing special about break exits compared to return or
> But Nick's doc addition and your alternative imply otherwise. One could
read Nick's statement and your paraphrase as suggesting that the else will
by executed if the loop is exited by return (like the finally of try) or
raise (like the except of try). And that is wrong.
An else clause on a try statement doesn't execute in any of those cases
either. I'm not assuming beginners are idiots, I'm assuming they're making
a perfectly logical connection that happens to be wrong.
Sent from my phone, thus the relative brevity :)
> Terry Jan Reedy
> Python-ideas mailing list
> Python-ideas at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas