1.5.2 for: else:

Tim Peters tim_one at email.msn.com
Sat Jul 31 01:15:54 EDT 1999
[William Tanksley]
> ...
> Oh, and if you REALLY like the current behavior of else-loop, you can
> still have it if else-loop were gone -- like this:
>
> try:
>  for x in list:
>    if iffers(x): raise "no!"
>    yadda(x)
> except "no!": pass
> else:
>  twiddle()
>
> That's two extra lines, and one level of nesting -- for the _exact_ same
> functionality, only much more likely to be comprehended.

Except that when I see that loop, I'm at a loss to guess whether the
"except" clause is intended to catch otherwise-uncaught "no!" exceptions
raised by list.__getitem__, iffers(), and yadda() too.  Presumably the
intent is that it should not, but there's no easy way to stop it from
catching unintended stuff too short of defining a unique exception for each
loop.  for+break+else doesn't have this problem.

use-exceptions-for-exceptions-and-break-for-things-that-shouldn't-ly y'rs  -
tim






More information about the Python-list mailing list