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

Stephen J. Turnbull stephen at xemacs.org
Fri Jun 8 14:53:18 CEST 2012

Rob Cliffe writes:
 > On 08/06/2012 10:04, Nick Coghlan wrote:

 > > Would it be worth adding the "except break:" clause to the language
 > > just to make it crystal clear what is actually going on?

-1 I understood what "except break:" was supposed to mean when I read
it the first time, but now I don't any more.

 > > I don't think so, but it's still a handy way to explain the
 > > semantics while gently steering people away from linking for/else
 > > and if/else too closely.

My main point about documentation is that for/else and if/else should
not be linked directly, but rather via while/else.

 > I think a better scheme would be to have more meaningful keywords or 
 > keyword-combinations, e.g.
 > for x in iterable:
 >      # do stuff
 > ifempty:  #  or perhaps ifnoiter: (also applicable to while loops)
 >      # do stuff
 > #ifbreak:
 >      # do stuff
 > #ifnobreak:
 >      # do stuff
 > which would give all the flexibility while making it reasonably clear 
 > what was happening.

Sure, but that's way overboard for something that's only rarely

More information about the Python-ideas mailing list