[Python-ideas] for/except/else syntax

George Sakkis george.sakkis at gmail.com
Thu Oct 8 16:07:02 CEST 2009


On Thu, Oct 8, 2009 at 5:51 AM, Carl Johnson
<cmjohnson.mailinglist at gmail.com> wrote:

> Yuvgoog Greenle:
>
>> -1 on for...then
>> Everything after the for loop is a "then":
>> for i in SEQ:
>>     A
>> # then
>> # code follows
>>
>> The special thing about "else" is that it's skipped upon break. That's the
>> *one and only* use case.
>> Since the current "else" clause only tests for "if not break", I think
>> spelling it out somehow could extremely improve "else" readability.
>> +1 for improving the readability of "for..break..else" and not just putting
>> the confusion under a "then" rug.
>
> Agreed. "Then" is not only a new keyword (=undesirable), it's not any
> more clear than what it replaces. It might lead to less
> misunderstanding of what the for-else clause does, but only because
> it's so opaque that those encountering it would be forced to look it
> up in the documentation rather than guess. But if that's the only
> advantage, you may as well name it "squazzlefritz" or something in
> Dutch. Or just do
>
> broke = False
> for item in items:
>    if cond(item)
>        broke = True
>        break
>
> if not broke:
>    #Pseudo else suite
>
> so that it's perfectly clear to the yahoo who comes after you what
> you're doing and they avoid the temptation to guess. In fact, that's
> what I would advise for most Python programmers to do today, unless
> they're completely confident that their code won't fall into the hands
> of yahoos at some point in the future.

Agreed. The more people disagree on how "for/else" should be spelled,
the more I think there is no keyword that can encapsulate what it does
unambiguously. Certainly "then" leaves me as clueless as "else" for
the reasons above. I was thinking of something verbose like
"elifnotbreak", but not only it is ugly, it is not 100% accurate; the
correct spelling would be "elifnotbreakandnotreturnandnotexception" ;)

George



More information about the Python-ideas mailing list