> On 2017 Mar 1 , at 4:37 a, Wolfgang Maier
> <[email protected]> wrote:
>
> I know what the regulars among you will be thinking (time machine, high bar
> for language syntax changes, etc.) so let me start by assuring you that I'm
> well aware of all of this, that I did research the topic before posting and
> that this is not the same as a previous suggestion using almost the same
> subject line.
>
> Now here's the proposal: allow an except (or except break) clause to follow
> for/while loops that will be executed if the loop was terminated by a break
> statement.
>
> The idea is certainly not new. In fact, Nick Coghlan, in his blog post
> http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html,
> uses it to provide a mental model for the meaning of the else following
> for/while, but, as far as I'm aware, he never suggested to make it legal
> Python syntax.
>
> Now while it's possible that Nick had a good reason not to do so, I think
> there would be three advantages to this:
>
> - as explained by Nick, the existence of "except break" would strengthen the
> analogy with try/except/else and help people understand what the existing
> else clause after a loop is good for.
> There has been much debate over the else clause in the past, most
> prominently, a long discussion on this list back in 2009 (I recommend
> interested people to start with Steven D'Aprano's Summary of it at
> https://mail.python.org/pipermail/python-ideas/2009-October/006155.html) that
> shows that for/else is misunderstood by/unknown to many Python programmers.
>
I’d like to see some examples where nested for loops couldn’t easily be avoided
in the first place.
> for n in range(2, 10):
> for x in range(2, n):
> if n % x == 0:
> print(n, 'equals', x, '*', n//x)
> break
> else:
> # loop fell through without finding a factor
> print(n, 'is a prime number')
Replace the inner loop with a call to any consuming a generator
for n in range(2,10):
if any(n % x == 0 for x in range(2,n)):
print('{} equals {} * {}'.format(n, x, n//x))
else:
print('{} is prime'.format(n))
>
> - it could provide an elegant solution for the How to break out of two loops
> issue. This is another topic that comes up rather regularly (python-list,
> stackoverflow) and there is again a very good blog post about it, this time
> from Ned Batchelder at
> https://nedbatchelder.com/blog/201608/breaking_out_of_two_loops.html.
> Stealing his example, here's code (at least) a newcomer may come up with
> before realizing it can't work:
>
> s = "a string to examine"
> for i in range(len(s)):
> for j in range(i+1, len(s)):
> if s[i] == s[j]:
> answer = (i, j)
> break # How to break twice???
Replace the inner loop with a call to str.find
for i, c in enumerate(s):
j = s.find(c, i+1)
if j >= 0:
answer = (i, j)
break
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/