[Python-ideas] for/except/else syntax

Stephen J. Turnbull stephen at xemacs.org
Thu Oct 8 08:29:34 CEST 2009

Adam Atlas writes:
 > On 7 Oct 2009, at 11:17, Andrey Fedorov wrote:
 > > Agree with Rob that the "else" keyword confusing in the context of a  
 > > for loop. In my mind, "for each pebble in the bag, give it to Ben,  
 > > or else ..." has no clear semantic meaning. What do you mean, "or  
 > > else"?
 > That wouldn't have any particular meaning in Python either (in that  
 > the "else" clause would never execute).

No, the else clause (as currently defined) *always* executes.

 > > In my mind, better words for what "else" currently does seem to be  
 > > "afterwards", "atend", "ending", "thereafter", or "subsequently".
 > I don't think any of those really sound like what the "else" clause  
 > does... they all just sound like something to execute when the loop is  
 > done, which is what happens after the loop body anyway. Switching to  
 > any of those words would probably only make it more unintuitive.

Since your intuition as expressed above is 100% wrong, I don't see how
switching to any of those words could possibly hurt. :-)  See Steven
d'Aprano's post advocating an alternative spelling of "else" as "then"
(which is a much more compact way of saying "subsequently").

Since IIRC we *still* have only one at all common use case for this
facility, which looks like

for thing in iterable:
    if thing.looks_good():
    thing = default_thing

I'm -1 on all extensions, -1 on any multiword respelling of "else", -1
on any spelling of "else" that includes the substring "break", and -0
on renaming "else" to "then" (or something like it).

More information about the Python-ideas mailing list