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")
 

On Tue, 14 Jul 2020 at 05:33, Ethan Furman <ethan@stoneleaf.us> wrote:
On 07/11/2020 09:16 PM, Rob Cliffe via Python-ideas wrote:

> My gut feeling (backed by no evidence) is that dealing with the case of zero iterations is not needed frequently enough to cater for it.

My personal experience is that the case of no iterations is frequent enough, and a big enough pain to deal with, that if we're making changes we should include a way to deal with it.  Currently:

     if empty_iterable:
         # do whatever
     else:
         for it in iterable:
             # do something

or maybe:

     obj = object
     for obj in some_iterator:
         do_stuff()
     if obj is object:
         # iterator was empty
         do_different_stuff()

Either way,

     for obj in iterator:
         do_stuff
     elif empty:
         do_other_stuff()

would be much nicer.

--
~Ethan~
_______________________________________________
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/W4C6TNXU7XRUA4XKY2YEQKOENFVTUT25/
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.