[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    Re: [Python-ideas] Nudging beginners towards a more accurate mental model for loop else clauses
From:       Bruce Leban <bruce () leapyear ! org>
Date:       2012-06-10 16:23:13
Message-ID: CAGu0AnsjRFdAroL5g-3k4TuDpzf1mA1gdabTScnrmdLnZt8A=Q () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


 On Sat, Jun 9, 2012 at 10:48 AM, Steven D'Aprano <steve@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@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

[Attachment #5 (text/html)]

<br class="Apple-interchange-newline"><div><br></div><div> On Sat, Jun 9, 2012 at \
10:48 AM, Steven D&#39;Aprano <span dir="ltr">&lt;<a \
href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>&gt;</span> \
wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div \
class="im">Loops exit at the top, not the bottom. This is most obvious when you think \
about a while loop:</div>

<br>while condition:<br>   ...<br><br><br>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.<br>

<br>Whether or not it is the *simplest* way to think about for/else, talking about \
exhaustion of the list (iterable) is correct.<br \
class="Apple-interchange-newline"></blockquote><div><br></div><div>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 &#39;loop exits normally&#39; would be much \
easier for them to understand.</div>

<div><br></div><div><br></div><div>On Sat, Jun 9, 2012 at 10:49 AM, Steven \
D&#39;Aprano <span dir="ltr">&lt;<a href="mailto:steve@pearwood.info" \
target="_blank">steve@pearwood.info</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


<div class="im">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.</div>

<br>- If the loop is exited by a return, *nothing* following the return is executed. \
That includes the else block.<br><br>- If execution is halted by an exception \
(including raise), *nothing* following the exception is executed. That includes the \
else block.<br>

<br>- If execution is halted by an external event that halts or interrupts the Python \
process, *nothing* following executes. That includes the else block.<br><br>- If the \
loop never completes, *nothing* following the loop executes. That includes the else \
block.<br>

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

<div><br></div><div><span \
style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"> \
Holly: He&#39;s dead, Dave. Everybody is dead. Everybody is dead, Dave.</span><br \
style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">


<span style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"> \
Lister: Wait. Are you trying to tell me everybody&#39;s dead?</span> </div><div><span \
style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"> \
Holly: Yup. Well, except for Dracula who was executing a try/finally. He&#39;s undead \
and probably going to kill you too. But I didn&#39;t want to bother you with that \
minor detail.</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div><span \
style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">:-)</span></div>


<div><div><br></div></div><div><font face="arial, helvetica, sans-serif">--- \
Bruce</font><div><font face="arial, helvetica, sans-serif">Follow me: <a \
href="http://www.twitter.com/Vroo" target="_blank">http://www.twitter.com/Vroo</a> <a \
href="http://www.vroospeak.com" \
target="_blank">http://www.vroospeak.com</a></font></div>

<div><font face="arial, helvetica, sans-serif"><br></font></div><br>
<br> </div></div>



_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic