[Python-ideas] Break multiple loop levels

David Mertz mertz at gnosis.cx
Sat May 11 23:15:48 EDT 2019


Ok, sure. But what if different seen_enough() conditions are in the two
inner loops? By "break to outer" ... I mean, don't get any more 'main' from
'stuff'. I meant to dig through some real code, but forgot because I was
writing code for work. This example on my tablet is the general pattern I
often need though. And obviously I've solved it one way or another hundreds
of times.

I think a "labelled break" would be nice though. It might have even been a
past PEP.

On Sat, May 11, 2019, 10:51 PM Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, May 12, 2019 at 12:43 PM David Mertz <mertz at gnosis.cx> wrote:
> >
> > Terry reminds me of a common case I encounter that cannot be transformed
> into a loop over itertools.product(). E.g.
> >
> > for main in stuff:
> >     if thing_about(main):
> >         for detail in more_stuff(stuff, main):
> >             if seen_enough(detail):
> >                 # break to outer somehow
> >     else:
> >         for detail in different_stuff():
> >             if seen_enough(detail):
> >                 # break to outer somehow
> >
>
> For that kind of loop, there's a different problem, which is the
> duplication. So I would start by rewriting that as:
>
> for main in stuff:
>     if thing_about(main):
>         details = more_stuff(stuff, main)
>     else:
>         details = different_stuff()
>     for detail in details:
>         if seen_enough(detail):
>             ...
>
> I'm not sure what you mean by "break to outer somehow", though, so I
> don't know what you're actually needing here. Concrete examples would
> help.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190511/40a630f2/attachment-0001.html>


More information about the Python-ideas mailing list