[Python-ideas] for/except/else syntax

Carl Johnson cmjohnson.mailinglist at gmail.com
Thu Oct 8 11:51:20 CEST 2009

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

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.


More information about the Python-ideas mailing list