1.5.2 for: else:

William Tanksley wtanksle at dolphin.openprojects.net
Sat Jul 31 20:13:47 EDT 1999


On Sat, 31 Jul 1999 01:15:54 -0400, Tim Peters wrote:
>[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.

Very good point, Tim.

The best solution of all is to simply not use features of the language
which I find objectionable for whatever reason.  I'm just disappointed in
the odd definition of for-else.  I'm glad to have learned it this way
rather than by painful debugging, and I don't think any of us are likely
to forget now :).

Memo to self -- don't use for-else, it doesn't do what it looks like it's
doing.

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list