[Python-ideas] for/else statements considered harmful

Nick Coghlan ncoghlan at gmail.com
Thu Jun 7 15:04:29 CEST 2012


On Thu, Jun 7, 2012 at 10:01 PM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
> On Thu, Jun 7, 2012 at 6:50 AM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
>> The reason they make such mistakes is that there's a strong
>> association of "else" with "if-then-else", and for many people that
>> seems to be somewhere between totally useless and actively misleading.
>
> I know it's really bad form to shift goalposts, but I can't help but
> offer an alternative hypothesis: What if it isn't that else is
> confusing, but that use of else is rare? People have lots of silly
> beliefs about things they never use, or haven't used in a very long
> time.

FWIW, I just added the following paragraph to the relevant section of
the Python tutorial in 2.7, 3.2 and 3.3:

=================
When used with a loop, the ``else`` clause has more in common with the
``else`` clause of a :keyword:`try` statement than it does that of
:keyword:`if` statements: a :keyword:`try` statement's ``else`` clause runs
when no exception occurs, and a loop's ``else`` clause runs when no ``break``
occurs. For more on the :keyword:`try` statement and exceptions, see
:ref:`tut-handling`.
=================

The new text should appear in the respective online versions as part
of the next daily docs rebuild.

It may not help much, but it won't hurt, and the "exceptional else" is
a much better parallel than trying to make loop else clauses fit the
"conditional else" mental model.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list