On Tue, 14 Jul 2020 at 12:38, Dominik Vilsmeier <dominik.vilsmeier@gmx.de> wrote:

On 14.07.20 09:54, Mathew Elman wrote:

What about adding `except` to the compound loop statement?
That way in cases where there needs to be clarity you can raise a specific exception rather than just `break`.
Keeping the logic of why you "break" the loop inside the loop and would also allow multiple reasons for breaking from a for loop to remain clear.

e.g.
 
for i  in range(N):
    if i > 3:
        raise ValueError
except ValueError:
    print(i) # >> 4
else:
    print("Loop not entered")
 

That can be done already today by putting the `for` loop in the `try` body.

 It can, but my thought was that this would be sugar that made it easier to follow the logic

Also here `else` should rather mean `did not raise` as for the normal `try/except/else` usage (and similar to how `for/else` means `did not break`).

Yes sorry, I made an error. I confused myself about what else does, how ironic. This would be most useful for cases where break and not breaking were unclear.
 

The more interesting part is to detect whether the loop did some work at all (i.e. whether the iterable was empty).

 This could be done by raising a "silent" exception that is handled by default (something like StopIteration), when trying to iterate an empty iterable. If the "NoIteration" exception is not caught explicitly it is ignored.
Then you could have :
for i in range(N):
     if i > 3:
        raise ValueError
except ValueError:
    print(f"{i} is more than 3")
except NoIteration:
    print("Loop not entered")
else:
    print(f"{i} is less than 4")

This may not be to everyone's taste, but to me it's better than the current state of things and clearer than having special if statements after a loop.

But also this can be done with some small overhead:

    loop = Loop(iterable)
    for x in loop:
        pass
    if loop.empty:
        pass

The `Loop` class here wraps the sentinel logic required to detect if the iterable was empty.

_______________________________________________
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/ICALEMWZK4RCAKVENV2VRJ7POEQXKSN6/
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.