[Python-ideas] for/else statements considered harmful
MRAB
python at mrabarnett.plus.com
Thu Jun 7 18:45:22 CEST 2012
On 07/06/2012 17:32, MRAB wrote:
> On 07/06/2012 16:52, Alice Bevan–McGregor wrote:
>> On 2012-06-07 15:30:11 +0000, Mike Meyer said:
>>> Calling it "wrap-up processing" seems likely to cause people to think
>>> about it as meaning "finally". But if the else clause is not executed
>>> if the except clause is (as done by try/except/else), then there's no
>>> longer an easy way to describe it.
>>>
>>> It seems like adding an except would change the conditions under which
>>> the else clause is executed (unlike try/except/else), as otherwise
>>> there's no easy way capture the current behavior, where else is
>>> executed whenever there are no chunks left to process. But that kind
>>> of things seems like a way to introduce bugs.
>>
>> Well, how about:
>>
>> for<var> in<iterable>:
>> pass # process each<var>
>> except: # no arguments!
>> pass # nothing to process
>> else:
>> pass # fell through
>> finally:
>> pass # regardless of break/fallthrough/empty
>>
>> Now for loops perfectly match try/except/else/finally!>:D (Like
>> exception handling, finally would be called even with an inner return
>> from any of the prior sections.)
>>
> Is the "finally" clause really necessary? Is it just the same as putting it
> after the loop?
>
I've just noticed your remark about the finally clause being run even
if there's a return. I can't say I like that; that's the job of
try...finally.
More information about the Python-ideas
mailing list