Being able to break multiple loops and having "labelled" breaks would be achievable using `except`, i.e. adding `except` to the loop statements before `else` like this:

for elem in iterable:
    ...
    if should_break(elem):
        raise SomeException
except SomeException as e:
    handle_break_behaviour(e)
else:
    print("Did not break")

would be sugar for:

try:
    for elem in iterable:
        ...
        if should_break(elem):
            raise SomeException
except SomeException as e:
    handle_break_behaviour(e)
else:
    print("Did not break")
 
I (and others) have suggested this before and no one has said it's a bad option, it's just been ignored, despite seeming to be an intuitive way to accomplish `else` clarity, "labelled" breaks and breaking from multiple loops. Is there a reason that this suggestion is worse / no better than adding special break syntax?

As for an example for labelled breaks how about something of the form:

def insert_ordered_no_duplicates(duplicate_free_list, item):
    for i, elem in enumerate(duplicate_free_list):
        if elem == item:
            break already_present
        if elem > item:
            break location_found
    case already_present:
        return duplicate_free_list
    case location_found:
        new_list = duplicate_free_list[:]
        new_list.insert(i, item)
        return new_list
    new_list = duplicate_free_list[:]
    new_list.append(item)
    return new_list
 
which you can conceivably have a nested case where you don't want to let duplicate inserts even be attempted like this:

def insert_many_ordered_strictly_no_duplicates(dup_free_list, items):
    new_list = dup_free_list[:]
    for item in items:
        for i, elem in new_list:
            if elem == item:
                break already_present
            if elem > item:
                break location_found
        case already_present:
            break duplicates_attempted
        case location_found:
            new_list.insert(i, item)
        else:
            new_list.append(item)
    case duplicates_attempted:
        return dup_free_list[:]
    return new_list


On Wed, 5 Aug 2020 at 13:40, Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
I note that both of these examples could be handled by having a way to break out of 2 loops at a time.
Rob

On 29/07/2020 12:33, Jonathan Fine wrote:
Thank you all, particularly Guido, for your contributions. Having some examples will help support the exploration of this idea.

Here's a baby example - searching in a nested loop. Suppose we're looking for the word 'apple' in a collection of books. Once we've found it, we stop.

    for book in books:
        for page in book:
            if 'apple' in page:
                break
        if break:
            break

However, suppose we say that we only look at the first 5000 or so words in each book. (We suppose a page is a list of words.)

This leads to the following code.

    for book in books:
        word_count = 0
        for page in book:
            word_count += len(page)
            if word in page:
                break
            if word_count >= 5000:
                break found
        if break found:
            break

At this time, I'd like us to focus on examples of existing code, and semantics that might be helpful. I think once we have this, the discussion of syntax will be easier.

By the way, the word_count example is as I typed it, but it has a typo. Did you spot it when you read it? (I only noticed it when re-reading my message.)

Finally, thank you for your contributions. More examples please.

-- 
Jonathan





_______________________________________________
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/ECALTXEP7M3YWAQCHLPRWPBJRQKQICBC/
Code of Conduct: http://python.org/psf/codeofconduct/

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