These are all a little bit ugly IMO. I've never written the
contextlib.suppress style, but I have the others. Usually what I'll
actually do is:
for x in stuff:
break_outer = False
for y in other_stuff:
do_whatever(x, y)
if cond:
break_outer = True
break
if break_outer: break
I think this is also ugly noise, but it feels like less of a cognitive load
than Oscar's approaches.
On Wed, Dec 4, 2019 at 5:56 PM Oscar Benjamin <[email protected]>
wrote:
> On Wed, 4 Dec 2019 at 21:16, Anders Hovmöller <[email protected]> wrote:
> > > On 4 Dec 2019, at 21:28, Soni L. <[email protected]> wrote:
> > >> On 2019-12-04 5:12 p.m., Mike Miller wrote:
> > >>
> > >>> On 2019-12-04 11:05, David Mertz wrote:
> > >>> I've often wanted named loops. I know approaches to this have been
> proposed many times, and they all have their own warts. E.g. an ad hoc
> pseudo code that may or may not match any previous proposal:
> > >>>
> > >>> for x in stuff as outer:
> > >>> for y in other_stuff as inner:
> > >>> ...
> > >>> if cond:
> > >>> break outer
> > >>>
> > >>> But we all manage without it.
> > >>
> > >> +1 Nice, find myself with that problem about once a year and it is
> annoying to code around.
> > >
> > > Just use context managers!
> >
> > What? How exactly? Can you rewrite the example given?
>
> I guess what is meant is this:
>
> from contextlib import suppress
>
> class Exit(BaseException):
> pass
>
> stuff = [10, 20, 30]
> other_stuff = [1, 2, 3]
>
> with suppress(Exit):
> for x in stuff:
> for y in other_stuff:
> print(x, y)
> if x + y > 21:
> raise Exit
>
> You can so the same with try/except:
>
> class Exit(BaseException):
> pass
>
> stuff = [10, 20, 30]
> other_stuff = [1, 2, 3]
>
> try:
> for x in stuff:
> for y in other_stuff:
> print(x, y)
> if x + y > 21:
> raise Exit
> except Exit:
> pass
>
> I dislike both of the above though. My suggestion would be to use a
> function rather than exceptions:
>
> stuff = [10, 20, 30]
> other_stuff = [1, 2, 3]
>
> def func():
> for x in stuff:
> for y in other_stuff:
> print(x, y)
> if x + y > 21:
> return
>
> func()
>
> In this example it might seem awkward to introduce a function just for
> this but normally in context the function can have a more reasonable
> purpose and a good name and return something useful etc.
>
> --
> Oscar
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/5FMNLSRDHAAP5T2DCUSNH7QGDIK6G2O6/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/MFDPITYTTPXY2PFQQ53WHH4J3FIZUN2D/
Code of Conduct: http://python.org/psf/codeofconduct/