[Python-ideas] Nudging beginners towards a more accurate mental model for loop else clauses

Steven D'Aprano steve at pearwood.info
Sun Jun 10 05:03:46 CEST 2012

Devin Jeanpierre wrote:
> Please stop mocking your own writing. I wrote nothing like the above.
> I said that maybe we should be specific and correct with when else is
> called. I didn't say that we should be exhaustive for when it is not.
> In fact, the example explanations I gave were not exhaustive.

You explicitly worried that users will conclude that the else block will run 
"except not when left by break", and stated that the description given earlier 
implies that for/else behaves like try/finally (i.e. that the else clause is 
*only* skipped on a break, but not return or raise).

There is no evidence that users somehow get the impression that for/else 
behaves like try/finally, and I find it completely implausible that they will 
do so in the future. If I'm wrong, the docs can be revised, but until then, in 
my opinion worrying about this is a documentation case of YAGNI.

The current documentation for for/else is already specific and correct. The 
real-life problem Nick is trying to solve is that many people think that the 
else clause implies that it behaves like if/else, and Nick is trying to nudge 
users to think of try/else instead. I think that's a worthy goal. Worrying 
about users reading the tutorial and concluding that for/else will run when 
you exit with a return, not so much.


More information about the Python-ideas mailing list