for what are for/while else clauses

Mel Wilson mwilson at the-wire.com
Wed Nov 19 21:14:27 EST 2003


In article <mailman.787.1069069214.702.python-list at python.org>,
"Fredrik Lundh" <fredrik at pythonware.com> wrote:
>for a while-statement, the controlling condition is the test at
>the top.
>
>for a for loop, the condition is "is there another item" (to quote the
>language reference: "When the items are exhausted (which is imme-
>diately when the sequence is empty) ... the loop terminates.".
>
>for a try-except clause, the condition is "did the suite raise an
>exception".

   Interesting way to think about it.  Thanks.

   So in some sample code:

        while some_condition:
            some_action ()
        else:
            last_action ()
        following_code ()

If the loop results in some_action being done, say, 17
times; then that means that some_condition was found true 17
times.  The 18th time, some_condition is found to be false,
the else takes effect and last_action gets done one time.
The only wrinkle then is that the while loop construct
passes control to the following code after that one
last_action.  But we expect that from a while loop.

   The effect of a break in the suite controlled by the
while is to blow away execution of the whole while
construct, including the else.

   As an explanation, I like it.

        Regards.        Mel.




More information about the Python-list mailing list