[Python-ideas] for/else statements considered harmful

Andre Roberge andre.roberge at gmail.com
Thu Jun 7 04:01:36 CEST 2012

On Wed, Jun 6, 2012 at 10:59 PM, Don Spaulding <donspauldingii at gmail.com>wrote:

> On Wed, Jun 6, 2012 at 7:15 PM, MRAB <python at mrabarnett.plus.com> wrote:
>> On 07/06/2012 00:20, Alice Bevan–McGregor wrote:
>>> Howdy!
>>> Was teaching a new user to Python the ropes a short while ago and ran
>>> into an interesting headspace problem: the for/else syntax fails the
>>> obviousness and consistency tests.  When used in an if/else block the
>>> conditional code is executed if the conditional passes, and the else
>>> block is executed if the conditional fails.  Compared to for loops
>>> where the for code is repeated and the else code executed if we
>>> "naturally fall off the loop".  (The new user's reaction was "why the
>>> hoek would I ever use for/else?")
>>>  I find the easiest way to think of it is imagine you're searching a
>> list. If you find what you're looking for you break, else you do
>> something else.
> I think the problem is that "break" doesn't sound like a positive, it
> sounds like a negative, and indeed it means we effectively *ignore* the
> rest of the list.  So when you get to the "else" it's like an English
> double-negative, awkward to understand.  Perhaps even more because you're
> effectively else-ing the break, not the for, so the indentation level even
> seems off.
> Backwards-compatibility issues aside, renaming "else" to "finally" sounds
> like a really great idea.

No: "finally" implies that it is going to be done at the end of the block;
the "else" clause is *not* executed if a break occurs - hence it has a
different semantics.

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120606/e9ef19b1/attachment.html>

More information about the Python-ideas mailing list