[Python-ideas] for/else statements considered harmful

Don Spaulding donspauldingii at gmail.com
Thu Jun 7 03:59:15 CEST 2012

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120606/fc9c928c/attachment.html>

More information about the Python-ideas mailing list