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 another iterator that 
would yield everything as the original iterator, but it would also be 
controllable through loop_control_object.


Sample implementation:

> def make_control_object(iterator):
>control_object = ControlObject()
>control_object.running = True
>def breakable_iterator():
>for x in iterator:
>yield x
>if not control_object.running:
>return
>return breakable_iterator(), control_object

The control object could have methods that allow sophisticated loop 
control, such as breaking, continuing, injecting exceptions, counting 
iterations, skipping etc.


The keywords 'break loop_control_object' and 'continue 
loop_control_object' could be simply implemented as:


> loop_control_object.do_break()



On Sun, May 12, 2019, 5:36 PM Paul Moore > wrote:


On Sun, 12 May 2019 at 21:06, David Mertz mailto:me...@gnosis.cx>> 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.

If you think in terms of "named loops" rather than "labels", "as"
makes a lot more sense.


Every name created with 'as' currently is a regular Python object, with 
a memory address, and some methods, that can participate in comparison 
(at least equality), that has a type, can be passed as a function 
argument, etc. Actually, all that is true of EVERY name in Python other 
than keywords.


It seems like none of this would be true of a loop name/label. Hence my 
first thought that a different way of introducing the word is more 
clear... Maybe we can just prefix lines with labels instead, and add a 
'goto' command :-)


___
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/


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 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.
>>
>> If you think in terms of "named loops" rather than "labels", "as"
>> makes a lot more sense.
>
>
> Every name created with 'as' currently is a regular Python object, with a
> memory address, and some methods, that can participate in comparison (at
> least equality), that has a type, can be passed as a function argument,
> etc. Actually, all that is true of EVERY name in Python other than keywords.
>
> It seems like none of this would be true of a loop name/label. Hence my
> first thought that a different way of introducing the word is more clear...
> Maybe we can just prefix lines with labels instead, and add a 'goto'
> command :-)
> ___
> 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/


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.
>
> If you think in terms of "named loops" rather than "labels", "as"
> makes a lot more sense.


Every name created with 'as' currently is a regular Python object, with a
memory address, and some methods, that can participate in comparison (at
least equality), that has a type, can be passed as a function argument,
etc. Actually, all that is true of EVERY name in Python other than keywords.

It seems like none of this would be true of a loop name/label. Hence my
first thought that a different way of introducing the word is more clear...
Maybe we can just prefix lines with labels instead, and add a 'goto'
command :-)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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) == 'foo':
>> for line in fh:
>> if process_foo(line) == -1:
>> break HAYSTACKS
>> elif get_format(header) == 'bar':
>> for line in fh:
>> if process_bar(line) == -1:
>> break HAYSTACKS
>>
>
> Nice.  Suggestion:
> 1. replace "label HAYSTACKS" with "as HAYSTACKS", if possible, so no new
> keyword is introduced;
> 2. replace "break HAYSTACKS" with "break from HAYSTACKS"
>

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.

Likewise, 'from' isn't really very similar to other existing uses.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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
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  wrote:

> On Mon, May 13, 2019 at 3:26 AM 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 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/


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 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
>
> If I had a "labelled break" feature, I might rewrite it something like the
> following (picking convenient syntax that I'm not per-se advocating as
> best, even if the concept is adopted):
>
> # 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) == 'foo':
> for line in fh:
> if process_foo(line) == -1:
> break HAYSTACKS
> elif get_format(header) == 'bar':
> for line in fh:
> if process_bar(line) == -1:
> break HAYSTACKS
>
>
Nice.  Suggestion:
1. replace "label HAYSTACKS" with "as HAYSTACKS", if possible, so no new
keyword is introduced;
2. replace "break HAYSTACKS" with "break from HAYSTACKS"



> In any case, it's often possible to either (a) redefine the inner loop
>>> as some sort of container operation, or (b) redefine the outer
>>> operation as a function, and use "return". So it would be helpful to
>>> have an example that CAN'T be rewritten in one of those ways, to use
>>> as a compelling example.
>>>
>>
> --
> 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
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 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

If I had a "labelled break" feature, I might rewrite it something like the
following (picking convenient syntax that I'm not per-se advocating as
best, even if the concept is adopted):

# 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) == 'foo':
for line in fh:
if process_foo(line) == -1:
break HAYSTACKS
elif get_format(header) == 'bar':
for line in fh:
if process_bar(line) == -1:
break HAYSTACKS

In any case, it's often possible to either (a) redefine the inner loop
>> as some sort of container operation, or (b) redefine the outer
>> operation as a function, and use "return". So it would be helpful to
>> have an example that CAN'T be rewritten in one of those ways, to use
>> as a compelling example.
>>
>
-- 
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
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 of contiguous loops to
> break out of and `break 1` is synonymous with `break`. Seems easier on my
> eyes/noggin than counting out the individual `break` statements.

   This is very much error-prone because on any refactoring (increasing
or decreasing the number of loop levels) one must increase/decrease all
numbers in internal loops.

   Labels are at least stable.


Indeed.

Go has labels for loops:

 Label:
 for { ...
   break Label
 }

as does Rust:

 'label: while condition:
{
 ...
 break label
 }

We can't use the same syntax in Python, but we might write it like this:

 @label while condition:
 block
 break label

and similar with for loops:

 @outer for x in range(100):
 @inner for y in range(3):
 if x == y == 1: break  # like ``break inner``
 if x == y == 2: break outer
 print(x, y)
 # break inner jumps to here
 # break outer jumps to here


@ reminds me too much of decorators.

How about this:

 while condition:
block
break label


 for x in range(100):
 for y in range(3):
if x == y == 1: break  # like ``break inner``
if x == y == 2: break outer
print(x, y)
# break inner jumps to here
# break outer jumps to here
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 test in the outer loop, 
like: `while dividend`. It would make clear what this algorithm is 
actually doing.


Sure, it could be refactored, but it would lose the relative simplicity. 
For such operations it is sometimes important to remain close to the raw 
mathematical notation.





def __divmod__(self, other):
dividend = self.canonical()

try:
divisor = other.canonical()
except AttributeError:
return NotImplemented

if not dividend:
return Field.zero(self.size), Field.zero(self.size)

try:
d = next(iter(divisor)) # leading term of the divisor
except TypeError:
d = self.const(divisor)

do = self.__monomial_order(d)

result = Field.zero(self.size)
running = True
while running: # outer loop, retry the changing `dividend`
for x in dividend: # yield all monomials from this polynomial
if self.__monomial_order(x) < do:
# encountered monomial of too small order, finish
running = False
break # exit outer loop
try:
c = self.__monomial_division(x, d) # may fail
assert c # sanity check
result += c
dividend -= c * divisor
dividend = dividend.canonical()
# if `dividend` nonzero, try again
# if `dividend` is zero, finish
running = bool(dividend)
break
except ArithmeticError:
# monomial division failed, try next one
continue # inner loop
else:
# `dividend` exhausted, finish
running = False
pass

if not hasattr(result, 'operator'):
result = self.const(result)

return result.canonical(), dividend.canonical()

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 of and `break 1` is synonymous with `break`. Seems easier on my
> > eyes/noggin than counting out the individual `break` statements.
> 
>This is very much error-prone because on any refactoring (increasing
> or decreasing the number of loop levels) one must increase/decrease all
> numbers in internal loops.
> 
>Labels are at least stable.

Indeed.

Go has labels for loops:

Label:
for { ...
  break Label
}

as does Rust:

'label: while condition:
   {
...
break label
}

We can't use the same syntax in Python, but we might write it like this:

@label while condition:
block
break label

and similar with for loops:

@outer for x in range(100):
@inner for y in range(3):
if x == y == 1: break  # like ``break inner``
if x == y == 2: break outer
print(x, y)
# break inner jumps to here
# break outer jumps to here


-- 
Steven
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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
> eyes/noggin than counting out the individual `break` statements.

   This is very much error-prone because on any refactoring (increasing
or decreasing the number of loop levels) one must increase/decrease all
numbers in internal loops.

   Labels are at least stable.

> Eli

Oleg.
-- 
Oleg Broytmanhttps://phdru.name/p...@phdru.name
   Programmers don't die, they just GOSUB without RETURN.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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, and because the details would muddy things. "Trimmed from real" won't 
> look much different than "invented from scratch".
>
> And yes, OF COURSE everything CAN be rewritten. But that's true in regards to 
> *every* proposed addition. The sentinel break_outer variable is often the was 
> to do it without really big restructuring (but with a handful of ugly 
> bookkeeping lines instead).
>

Yes - that's exactly the sort of thing that makes a great example.
Show the way you'd LIKE to write it, and then show how you have to
write it using current tools. If the latter is really ugly, requires
finicky bookkeeping, or is error-prone, it's a good argument in favour
of the new feature.

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/


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" won't look much different than "invented from scratch".

And yes, OF COURSE everything CAN be rewritten. But that's true in regards
to *every* proposed addition. The sentinel break_outer variable is often
the was to do it without really big restructuring (but with a handful of
ugly bookkeeping lines instead).

On Sat, May 11, 2019, 11:22 PM Chris Angelico  wrote:

> 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 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.
> >
>
> This is why concrete examples are much easier to discuss, heh.
>
> In any case, it's often possible to either (a) redefine the inner loop
> as some sort of container operation, or (b) redefine the outer
> operation as a function, and use "return". So it would be helpful to
> have an example that CAN'T be rewritten in one of those ways, to use
> as a compelling example.
>
> 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/


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 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.
>

This is why concrete examples are much easier to discuss, heh.

In any case, it's often possible to either (a) redefine the inner loop
as some sort of container operation, or (b) redefine the outer
operation as a function, and use "return". So it would be helpful to
have an example that CAN'T be rewritten in one of those ways, to use
as a compelling example.

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/


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 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@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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:
for detail in different_stuff():
if seen_enough(detail):
# break to outer somehow


On Sat, May 11, 2019, 6:58 PM Terry Reedy  wrote:

> 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
> > break continue
> > break break continue
> > ...
> >
> > And so on.
> >
> > Example:
> >
> > for i in range(10):
> >  for j in range(10):
> >  if i == 2 and j == 3:
> >  break break
> >
> > for i in range(10):
> >  for j in range(10):
> >  if i == 2 and j == 3:
> >  break continue
> >  if i == 7:
> >  break break
>
> > Breaking out from many loops at once is a common practice, currently
> > implemented through boolean testing and exceptions.
>
> Python exceptions generalize both 'return' and 'break' as methods for
> escaping nested contexts and are intended for generalized flow control.
>
> > This proposal would make it cleaner.
>
> I actually think that the general method is 'cleaner', be separating
> 'leave here' and 'arrive here'.  This proposal would mean typing fewer
> words in the special case where there are nested loops within one function.
>
> The cost is a) more syntax to learn and b) tightly tying together the
> two loops more tightly than might be necessary, and thereby inhibiting
> re-factoring.  In the examples above, where the loops are inherently
> tied together, the two loops can, as noted by others, be reduced to one.
>   If they are not inherently tied together, one might want to reduce the
> inner loop to a comprehension or wrap it in a new function so it can
> also be used elsewhere.  If the inner loop raises, wrapping or
> unwrapping it, as needed for reusability or efficiency, does not affect
> the logic.
>
> --
> Terry Jan Reedy
>
>
> ___
> 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/


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
break continue
break break continue
...

And so on.

Example:

for i in range(10):
     for j in range(10):
     if i == 2 and j == 3:
     break break

for i in range(10):
     for j in range(10):
     if i == 2 and j == 3:
     break continue
     if i == 7:
     break break


Breaking out from many loops at once is a common practice, currently 
implemented through boolean testing and exceptions.


Python exceptions generalize both 'return' and 'break' as methods for 
escaping nested contexts and are intended for generalized flow control.



This proposal would make it cleaner.


I actually think that the general method is 'cleaner', be separating 
'leave here' and 'arrive here'.  This proposal would mean typing fewer 
words in the special case where there are nested loops within one function.


The cost is a) more syntax to learn and b) tightly tying together the 
two loops more tightly than might be necessary, and thereby inhibiting 
re-factoring.  In the examples above, where the loops are inherently 
tied together, the two loops can, as noted by others, be reduced to one. 
 If they are not inherently tied together, one might want to reduce the 
inner loop to a comprehension or wrap it in a new function so it can 
also be used elsewhere.  If the inner loop raises, wrapping or 
unwrapping it, as needed for reusability or efficiency, does not affect 
the logic.


--
Terry Jan Reedy


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 and using "return" to break out of those loops. If you nest
the function in the spot where the loops would be and use nonlocal
statements, you can even modify variables in the surrounding code
(counters, etc.) or leave values behind.

If you're opposed to a function and performance is not critical [1],
you can also raise a custom "exception" at the multi-break spot and
handle it at the appropriate place in the outer code. This is useful
if you need to break out of a function plus additional nesting layers.

[1] I mention performance here because if I recall correctly,
exception handling is quite expensive and slow in Python.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 break twice semantics simultaneously.

```
for x in range(n):
for y in range(m):
break
else:
continue
break
```


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.
>
> -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 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.
>> >
>>
>> Do you have an example of such loops ?
>> ___
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Christopher Barker, PhD
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 

Regards
Srinivas Devaki
Software Developer at Zomato, New Delhi
Phone: +91 9491 383 249
Telegram: @eightnoteight
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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
> 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.
> >
>
> Do you have an example of such loops ?
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 do that.
> 

Do you have an example of such loops ?
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 that gets set back and
forth between True and False working the loops. Usually work a name that
descriptive of the actual domain of the code.

On Sat, May 11, 2019, 1:40 PM Paul Moore  wrote:

> 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
> nested loop, I've tended to find that it's probably more readable to
> refactor the code to use a generator, or something like that.
> Paul
> ___
> 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/


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
nested loop, I've tended to find that it's probably more readable to
refactor the code to use a generator, or something like that.
Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


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 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
> ...
>
> And so on.
>
> Example:
>
> for i in range(10):
>  for j in range(10):
>  if i == 2 and j == 3:
>  break break
>
> for i in range(10):
>  for j in range(10):
>  if i == 2 and j == 3:
>  break continue
>  if i == 7:
>  break break
>
> Breaking out from many loops at once is a common practice, currently
> implemented through boolean testing and exceptions. This proposal would
> make it cleaner.
>
> ___
> 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/


[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
...

And so on.

Example:

for i in range(10):
for j in range(10):
if i == 2 and j == 3:
break break

for i in range(10):
for j in range(10):
if i == 2 and j == 3:
break continue
if i == 7:
break break

Breaking out from many loops at once is a common practice, currently 
implemented through boolean testing and exceptions. This proposal would 
make it cleaner.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/