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.
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 email@example.com 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
obj = object for obj in some_iterator: do_stuff() if obj is object: # iterator was empty do_different_stuff()
for obj in iterator: do_stuff elif empty: do_other_stuff()
would be much nicer.
-- ~Ethan~ _______________________________________________ Python-ideas mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://firstname.lastname@example.org/message/W4C6TN... Code of Conduct: http://python.org/psf/codeofconduct/