But in `for...else` the `else` call isn't always called, so changing `else` for `finally` doesn't make sense. What you're suggesting is replacing `else` with `on_finish` and adding `finally` and `on_break`.

I agree that having `finally` could make the use cases of `else` clearer, but I am not convinced renaming "else" to "on_finish" would help the confusion for the 0 iteration case.

I think that since this suggestion doesn't help with the 0 iteration case (my first idea here didn't either), it feels like added extra compound statements need to be immediately intuitive to be worth having - either because they read like a sentence or parallel existing python e.g. `try-except-else-finally` or `if-elif-else` etc.


On Wed, 15 Jul 2020 at 06:47, Steve Barnes <GadgetSteve@live.co.uk> wrote:

Can I suggest that for loops the `else` would be a lot clearer if it was spelt `finally` as was done for PEP-0341 for try blocks and that we might possibly need one or more `on_…` clauses such as `on_break` and `on_finish` I think that this would be a lot clearer:

 

for i in range(N):

    if i > 3:

        break;

on_break:  # Called if loop was broken

    print(i)

on_finish:  # Called if loop was not broken

    print("Loop Completed")

finally:  # Always called (replaces for…else)

    print("Loop Ended")

 

Which I think would be a lot easier for newcomers to learn than try…for…else…except…else e.g.:

 

try:

    for i in range(N):

       if i > 3:

            break;

       elif i % 2 == 0:

            raise ValueError("Odds Only");

        else: # to if

            print(i)

    else:  # Else to loop

        print("Loop Completed")

except ValueError as err:

    print(err)

else:  # to try

    print("No Exception")

finally:

    print("Try Ended")

 

Where the multitude of elses makes my eyes cross.

 

Steve Barnes

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MKAAWV6OT7SRIHTDOAEA3OHV6ZLSGLE2/
Code of Conduct: http://python.org/psf/codeofconduct/

Notice:
This email is confidential and may contain copyright material of members of the Ocado Group. Opinions and views expressed in this message may not necessarily reflect the opinions and views of the members of the Ocado Group.

If you are not the intended recipient, please notify us immediately and delete all copies of this message. Please note that it is your responsibility to scan this message for viruses.

References to the "Ocado Group" are to Ocado Group plc (registered in England and Wales with number 7098618) and its subsidiary undertakings (as that expression is defined in the Companies Act 2006) from time to time. The registered office of Ocado Group plc is Buildings One & Two, Trident Place, Mosquito Way, Hatfield, Hertfordshire, AL10 9UL.