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

Devin Jeanpierre jeanpierreda at gmail.com
Sat Jun 9 16:01:50 CEST 2012


On Fri, Jun 8, 2012 at 10:31 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Why is it misleading? It is *incomplete* insofar as it assumes the reader
> understands that (in the absence of try...finally) a return or raise will
> immediately exit the current function regardless of where in the function
> that return/raise happens to be. I think that's a fair assumption to make.

How can the reader understand that, when the reader doesn't know that
return or raise exist yet? The assumption that the reader understands
basic Python is unreasonable. This is the tutorial.

As I understand the objection, it is misleading in that it puts the
focus on the wrong thing. It says "it's skipped by a break", as if
that were special. It's skipped by a lot of things that aren't
mentioned, the really interesting thing is when it _isn't_ skipped,
which is glossed over. It is implied that this happens whenever it is
exited by anything other than break, but of course that isn't true,
and you have to think "well, what about return and raise?"  However,
as mentioned above, no student will ever think about return and raise,
because those constructs have not been introduced yet. I wonder if
they will just internalize "except not when left by break"? That would
be awful!

Anyway, I'm not really an expert on writing technical documentation I
would expect that it's better to not force the reader to remember
information and think about implications, if we can say flat-out
exactly what happens. Even if they can do it successfully, surely it
is annoying?


If you want to mention break up-front, why not reverse the clause
order? Currently the phrasing is this:

    Loop statements may have an else clause; it is executed when the
loop terminates through exhaustion of the list (with for) or when the
condition becomes false (with while), but not when the loop is
terminated by a break statement.

It could also be (something like) this:

    Loop statements may have an else clause; it is not executed when
the loop is terminated by a break statement; it is only executed when
the loop terminates through exhaustion of the list (with for) or when
the condition becomes false (with while).

This reads backwards to me, because the clarifying information is
listed before the main fact. Also it's a terrible sentence (my fault,
the original was long but didn't have three independent clauses). But
hey.

Or you could split it up into two sentences:

    Loop statements may have an else clause, which is only executed
when the loop terminates through exhaustion of the list (with for) or
when the condition becomes false (with while). The else clause is
*not* executed when the loop is terminated by a ``break``, or any
other control flow construct you will see.

And so on. Lots of room to play around with how the information gets
across without sacrificing the core fact of how else works.

If that core fact is unworkable and does more harm than good, then I
guess it has to go, though. Lying-to-children is a well-worn and
useful didactic technique.

-- Devin



More information about the Python-ideas mailing list