For the likely rare situation where I'd want to do this rather than
refactoring into a function, I might try with something like this without
requiring changes to the language:

from contextlib import contextmanager

@contextmanager
def breakable():
    class Break(Exception): pass
    def breaker(): raise Break
    try:
        yield breaker
    except Break:
        pass

This allows defining a block that can be "aborted" (which can have a loop
or any other statement block within); you abort it by calling the resulting
value of the context manager. An example of use is available at
https://gist.github.com/dmoisset/55f5916f9f339a143b6f3d155de8706e




On Thu, 3 Dec 2020 at 11:07, Jonatan <pybots...@gmail.com> wrote:

> Hi, sometimes I do nested loops and I want to break specific loop,
> outer/inner etc.
> I need to do an ugly thing with for..else and it's annoying.
>
> It'd be nice if we could do so:
> for i in range(10) as loop_i:
>     for j in range(i, i + 10) as loop_j:
>         if i + j == 9:
>             break loop_i
> or something like this.
> Please comment what do you think about this idea.
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@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/55E2WXH6KLHZ67IFFCXRC7JIN4YB7EVR/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/VY4KQNADJYALC3MHIDGMJFX4DCF5IA4Q/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to