Re: [Python-ideas] Break multiple loop levels

2019-05-13 Thread haael
How about doing it pythonic way then, "everything is a hack on a symbol table". Let normal loops remain normal loops. Let's introduce a special construction: > for x in iterator as loop_control_object: > loop_body(loop_control_object) The iterator in the loop would be wrapped inside

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread James Lu
I think a Python version of longjmp() and setjmp() might be easier to understand. On Sun, May 12, 2019 at 6:23 PM David Mertz wrote: > On Sun, May 12, 2019, 5:36 PM Paul Moore wrote: > >> On Sun, 12 May 2019 at 21:06, David Mertz wrote: >> > I thought of 'as' initially, and it reads well as

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread David Mertz
On Sun, May 12, 2019, 5:36 PM Paul Moore wrote: > On Sun, 12 May 2019 at 21:06, David Mertz wrote: > > I thought of 'as' initially, and it reads well as English. But it felt > to me like the meaning was too different from the other meanings of 'as' in > Python. I might be persuaded otherwise. >

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread David Mertz
On Sun, May 12, 2019, 3:33 PM Gustavo Carneiro wrote: > # Hypothetical future labelled break: >> def find_needle_in_haystacks(): >> for haystack in glob.glob('path/to/stuff/*') label HAYSTACKS: >> fh = open(fname) >> header = fh.readline() >> if get_format(header) ==

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread David Mertz
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

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread Gustavo Carneiro
On Sun, 12 May 2019 at 18:26, David Mertz 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

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread David Mertz
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

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread MRAB
On 2019-05-12 10:44, Steven D'Aprano wrote: On Sun, May 12, 2019 at 11:16:21AM +0200, Oleg Broytman wrote: On Sun, May 12, 2019 at 01:36:28AM -0700, Elias Tarhini wrote: > If I may propose `break n` as a replacement for the original message's > `break break ... break`, where n>0 is the number

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread haael
The concrete example I was working on when I started to miss double break. This is an implementation of polynomial long division in Galois field. Almost unmodified. With outer break, I would't need to use the `running` variable. In fact, for mathematical clarity, I would like to put a

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread Steven D'Aprano
On Sun, May 12, 2019 at 11:16:21AM +0200, Oleg Broytman wrote: > On Sun, May 12, 2019 at 01:36:28AM -0700, Elias Tarhini > wrote: > > If I may propose `break n` as a replacement for the original message's > > `break break ... break`, where n>0 is the number of contiguous loops to > > break out

Re: [Python-ideas] Break multiple loop levels

2019-05-12 Thread Oleg Broytman
On Sun, May 12, 2019 at 01:36:28AM -0700, Elias Tarhini wrote: > If I may propose `break n` as a replacement for the original message's > `break break ... break`, where n>0 is the number of contiguous loops to > break out of and `break 1` is synonymous with `break`. Seems easier on my >

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Chris Angelico
On Sun, May 12, 2019 at 1:30 PM David Mertz wrote: > > I'll try to find a concrete one tomorrow. I did this recently, but I might > have factored it away somehow. The real life code has lots of extraneous > parts that need to be removed or simplified though. Both because they aren't > FLOSS,

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread David Mertz
I'll try to find a concrete one tomorrow. I did this recently, but I might have factored it away somehow. The real life code has lots of extraneous parts that need to be removed or simplified though. Both because they aren't FLOSS, and because the details would muddy things. "Trimmed from real"

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Chris Angelico
On Sun, May 12, 2019 at 1:16 PM David Mertz wrote: > > 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

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Chris Angelico
On Sun, May 12, 2019 at 12:43 PM David Mertz 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

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread David Mertz
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:

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Terry Reedy
On 5/11/2019 1:20 PM, haael wrote: Python allows for breaking out from a loop through 'break' and 'continue' keywords. It would be nice if it was possible to break many loop levels using one command. I propose two constructions for that: break break break break break break ... continue

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Jonathan Goble
On Sat, May 11, 2019 at 1:22 PM haael wrote: > Python allows for breaking out from a loop through 'break' and > 'continue' keywords. It would be nice if it was possible to break many > loop levels using one command. I'm surprised no one has yet mentioned splitting off the loops into a function

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread srinivas devaki
On Sun, May 12, 2019 at 3:45 AM Christopher Barker wrote: > > judicious use of the for loop "else" may help here, too -- I know I often forget it's there. yeah, I often do this when I want to break twice but this method only supports breaking twice at once but this doesn't support break once and

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Christopher Barker
judicious use of the for loop "else" may help here, too -- I know I often forget it's there. -CHB On Sat, May 11, 2019 at 2:26 PM Adrien Ricocotam wrote: > > > > On 11 May 2019, at 23:20, David Mertz wrote: > > > > I don't love the 'break break' syntax idea. But I find myself wanting to >

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Adrien Ricocotam
> On 11 May 2019, at 23:20, David Mertz wrote: > > I don't love the 'break break' syntax idea. But I find myself wanting to > break out of nested loops quite often. Very rarely could this be transformed > to an 'itertools.permutation' de-nesting, albeit perhaps more often than I > actually

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread David Mertz
I don't love the 'break break' syntax idea. But I find myself wanting to break out of nested loops quite often. Very rarely could this be transformed to an 'itertools.permutation' de-nesting, albeit perhaps more often than I actually do that. My usual kludge is a sentinel 'break_outer' variable

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Paul Moore
On Sat, 11 May 2019 at 18:22, haael wrote: > Breaking out from many loops at once is a common practice Do you have evidence for this? In my experience, it's very rare (although I concede it would be *occasionally* convenient). In most cases where I'd consider breaking out of multiple levels of

Re: [Python-ideas] Break multiple loop levels

2019-05-11 Thread Jonathan Fine
Dear Haael With python you can write for i, j in something (): # loop body This would cover both your examples. For the first, use itertools.product. For the second, write a custom iterator. -- Jonathan On Sat, 11 May 2019, 18:22 haael, wrote: > > Python allows for breaking out

[Python-ideas] Break multiple loop levels

2019-05-11 Thread haael
Python allows for breaking out from a loop through 'break' and 'continue' keywords. It would be nice if it was possible to break many loop levels using one command. I propose two constructions for that: break break break break break break ... continue break continue break break continue