Re: for what are for/while else clauses

Discussion in 'Python' started by Fredrik Lundh, Nov 18, 2003.

  1. Michael Chermside wrote:

    > According to this experiment, the only way to get "executed else clause"
    > to print is to set exitViaBreak to True. So I really don't understand what
    > you are claiming. Seems to me that the execution of the else does NOT
    > depend on the status of the loop condition at exit.


    That's not what your program shows, though: if you set "exitViaBreak"
    to true, your program sets the loop condition to false *before* it enters
    the loop. In other words, your experiment shows that the else clause
    runs when the loop condition is found to be false, and that it only runs
    in that case.

    Maybe you should read the C code, after all?

    Or maybe this quote from a popular python book might help:

    "Both the while and for statement may optionally have a trailing
    else clause. The statement or statements after the else execute
    when the loop terminates naturally (at the end of the for iterator
    or when the while loop condition becomes false), but not when
    the loop terminates prematurely (via break, return, or an exception).

    (the author seems to have rebooted since he wrote that, though.)

    </F>
     
    Fredrik Lundh, Nov 18, 2003
    #1
    1. Advertising

  2. Fredrik Lundh

    Georgy Pruss Guest

    "Fredrik Lundh" <> wrote in message news:...
    | <...>
    | Or maybe this quote from a popular python book might help:
    |
    | "Both the while and for statement may optionally have a trailing
    | else clause. The statement or statements after the else execute
    | when the loop terminates naturally (at the end of the for iterator
    | or when the while loop condition becomes false), but not when
    | the loop terminates prematurely (via break, return, or an exception).
    |
    | (the author seems to have rebooted since he wrote that, though.)
    |
    | </F>

    The 'else' part of for/while is VERY useful and this is of course a big
    plus for Python. But the name 'else' is VERY misleading and useless
    and is probable even worse than 'static' in C++. That's exectly why
    every week somebody asks this question here again and again.
    I'm sure, even something like 'on_nornal_exit' would be much much better.

    --
    Georgy Pruss
    E~mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base64')
     
    Georgy Pruss, Nov 18, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    1,323
    Jerry Coffin
    Feb 1, 2005
  2. Diez B. Roggisch

    for what are for/while else clauses

    Diez B. Roggisch, Nov 14, 2003, in forum: Python
    Replies:
    32
    Views:
    810
    Michele Simionato
    Nov 28, 2003
  3. Jyotirmoy Bhattacharya

    nested list comprehension and if clauses

    Jyotirmoy Bhattacharya, Jun 28, 2007, in forum: Python
    Replies:
    5
    Views:
    322
    Jyotirmoy Bhattacharya
    Jun 28, 2007
  4. Frank Stutzman
    Replies:
    6
    Views:
    484
    Marc Christiansen
    Nov 20, 2007
  5. Gergely Kontra
    Replies:
    16
    Views:
    232
    Devin Mullins
    Sep 20, 2005
Loading...

Share This Page