I thought of "we could return immediately" shortly after I posted it. Yes,
that's true in the example I wrote. But it's easy enough to vary it so that
something happens after the loop as well. I also noticed that for the
simple version, it might be slightly shorter to put the conditional inside
the loop rather than to decide between loops.

There are lots of ways to work around the absence of a labelled break. Many
times putting the inner loop in a comprehension can simplify things too,
for example (that's roughly what my actual code was refactored to, mostly
not just because of the break issue).

On Sun, May 12, 2019, 3:38 PM Chris Angelico <ros...@gmail.com> wrote:

> On Mon, May 13, 2019 at 3:26 AM David Mertz <me...@gnosis.cx> wrote:
> >
> > To be clear in this thread, I don't think I'm really ADVOCATING for a
> multi-level break.  My comments are simply noting that I personally fairly
> often encounter the situation where they would be useful.  At the same
> time, I worry about Python gaining sometimes-useful features that
> complicate the overall language and bring it closer to Perl's "everyone can
> write in their own style."
> >
> > So to answer Chris' question... well, i did find something recent, but
> it has a ton of other extraneous stuff.  But I've simplified to something
> that isn't that much different from my example from my tablet yesterday,
> but is fairly realistic still.
> >
> > I have an existing function that looks roughly like this:
> >
> > def find_needle_in_haystacks():
> >     found_needle = False
> >     for haystack in glob.glob('path/to/stuff/*'):
> >         fh = open(fname)
> >         header = fh.readline()
> >         if get_format(header) == 'foo':
> >             for line in fh:
> >                 status = process_foo(line)
> >                 if status = -1:
> >                     found_needle = True
> >                     break
> >         elif get_format(header) == 'bar':
> >             for line in fh:
> >                 status = process_bar(line)
> >                 if status = -1:
> >                     found_needle = True
> >                     break
> >
> >         if found_needle:
> >             break
> >
>
> Cool. How much code is there AFTER this loop? The most obvious way to
> handle this is to immediately 'return' instead of multi-level
> breaking.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to