But `finally` with a `for` loop is redundant since the code can be
placed just after the loop. For `try/except` it's a different situation
since the exception might bubble up, so "normal" code after the `try`
won't be reached.

Also `on_break` doesn't seem really important since that code can be
executed inside the loop right before the `break`.

We already have `else` for `on_finish` and I think when recalling the
analogy to exceptions it's not that confusing: a `try` block can be
exited either normally (because all code has been executed) or because
it raised an exception; here `else` means it exited normally. Similarly
a `for` loop can terminate either normally by executing all iterations
or because a `break` occurred; and similarly `else` means it terminated
normally.

On 15.07.20 08:47, Mathew Elman wrote:
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 <gadgetst...@live.co.uk
<mailto:gadgetst...@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
    <mailto:python-ideas@python.org>
    To unsubscribe send an email to python-ideas-le...@python.org
    <mailto:python-ideas-le...@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.


_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/3ZPJDOTFCCQALCHIVEIPFJBROG6JSHIY/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/W7M77OYSI4Z2TKPC7K7UIKUPEZVLRD77/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to