[Python-ideas] for/except/else

Rhodri James rhodri at kynesim.co.uk
Wed Mar 1 07:07:12 EST 2017


Much snippage; apologies, Wolfgang!
On 01/03/17 09:37, Wolfgang Maier wrote:
> 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.
[snip]
> - in some situations for/except/else would make code more readable by
> bringing logical alternatives closer together and to the same
> indentation level in the code. Consider a simple example (taken from the
> docs.python Tutorial:
>
> 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')
>
> There are two logical outcomes of the inner for loop here - a given
> number can be either prime or not. However, the two code branches
> dealing with them end up at different levels of indentation and in
> different places, one inside and one outside the loop block. This second
> issue can become much more annoying in more complex code where the loop
> may contain additional code after the break statement.
>
> Now compare this to:
>
> for n in range(2, 10):
>     for x in range(2, n):
>         if n % x == 0:
>             break
>     except break:
>         print(n, 'equals', x, '*', n//x)
>     else:
>         # loop fell through without finding a factor
>         print(n, 'is a prime number')
>
> IMO, this reflects the logic better.

It reads worse to me, I'm afraid.  Moving the "print" disassociates it 
from the condition that caused it, making it that bit harder to 
understand.  You'd have a more compelling case with a complex loop with 
multiple breaks all requiring identical processing.  However my 
experience is that such cases are rare, and are usually attempts to do 
exception handling with out actually using exceptions.  I'm not terribly 
inclined to help people make more work for themselves.

> - 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???
>
> with for/except/else this could be written as:
>
> s = "a string to examine"
> for i in range(len(s)):
>     for j in range(i+1, len(s)):
>         if s[i] == s[j]:
>             break
>     except break:
>         answer = (i, j)
>         break

That is a better use case.  I must admit I normally handle this sort of 
thing by putting the loops in a function and returning out of the inner 
loop.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-ideas mailing list