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

Bruce Leban bruce at leapyear.org
Sun Jun 10 18:23:13 CEST 2012


 On Sat, Jun 9, 2012 at 10:48 AM, Steven D'Aprano <steve at pearwood.info>
 wrote:

> Loops exit at the top, not the bottom. This is most obvious when you think
> about a while loop:
>
> while condition:
>    ...
>
>
> Of course you have to be at the top of the loop for the while to check
> condition, not the bottom. For-loops are not quite so obvious, but
> execution has to return back to the top of the loop in order to check
> whether or not the sequence is exhausted.
>
> Whether or not it is the *simplest* way to think about for/else, talking
> about exhaustion of the list (iterable) is correct.
>

If you want to talk about exhaustion of the list then you need to talk
differently about the while loop. Documentation is usually written for
non-experts. When I taught intro to programming, the mental model that most
students had was nowhere near as strong as most people on this list. The
concept 'loop exits normally' would be much easier for them to understand.


On Sat, Jun 9, 2012 at 10:49 AM, Steven D'Aprano <steve at pearwood.info>
 wrote:

> Ignoring try/finally blocks, which are special, we can assume that the
> reader has (or will have once they actually learn about functions and
> exceptions) a correct understanding of the behaviour of return and raise.
>
> - If the loop is exited by a return, *nothing* following the return is
> executed. That includes the else block.
>
> - If execution is halted by an exception (including raise), *nothing*
> following the exception is executed. That includes the else block.
>
> - If execution is halted by an external event that halts or interrupts the
> Python process, *nothing* following executes. That includes the else block.
>
> - If the loop never completes, *nothing* following the loop executes. That
> includes the else block.
>

You've written four different ways of saying 'loop does not exit normally'
vs. saying once 'loop exits normally'. When you emphasize *nothing* above,
it strongly suggests they all mean the same thing. If you *don't* ignore
try/finally, then they don't. I don't think documentation needs to cover
every case, but if you're going to write stuff in bold letters (or italic
or whatever), then readers expect you're covering all the bases and not
ignoring special cases. That may not be your intent but that's the way
people read things. Again, docs are written for non-experts.

   Holly: He's dead, Dave. Everybody is dead. Everybody is dead, Dave.
   Lister: Wait. Are you trying to tell me everybody's dead?
   Holly: Yup. Well, except for Dracula who was executing a try/finally.
He's undead and probably going to kill you too. But I didn't want to bother
you with that minor detail.

:-)

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120610/24b32a74/attachment.html>


More information about the Python-ideas mailing list