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

Mike Meyer mwm at mired.org
Fri Jun 8 21:53:05 CEST 2012


On Fri, 08 Jun 2012 21:53:18 +0900
"Stephen J. Turnbull" <stephen at xemacs.org> wrote:
> My main point about documentation is that for/else and if/else should
> not be linked directly, but rather via while/else.

Right. That was the most enlightening comment I saw in this
thread. Writing the if/else and while/else out as:

    if condition:
        # code to run if condition is true
    else:
        # code to run if condition is false

    while condition:
        # code to run while condition is true
    else:
        # code to run when condition is false

Seems obvious enough to me. For is a little bit harder, but still a
straightforward if you think about it in terms of the while.

    for x in iterable:
        # code to run while there are objects left in iterable
    else:
        # code to run when there are no objects left in iterable.

	<mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list