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. 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`). The more interesting part is to detect whether the loop did some work at all (i.e. whether the iterable was empty). 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.