"for" with "else"?

John Roth newsgroups at jhrothjr.com
Tue Sep 30 22:09:53 EDT 2003


"Stephen Horne" <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> wrote in
message news:2c9knv86m1il4ajng85655dm7u5j5b992i at 4ax.com...
> On Thu, 25 Sep 2003 11:15:51 -0400, "John Roth"
> <newsgroups at jhrothjr.com> wrote:
>
> >Frankly, I'd like to see special case syntax for an empty
> >sequence, but it's not that hard to handle now.
>
> That is what the 'else' suggests to me - particularly with 'while'
> loops. I don't think I've ever needed the existing 'else', but I could
> have used a loop-body-not-executed-at-all special case on a number of
> occasions
>
> I can see the use an application of 'else', of course - if you use
> 'break' to end a loop you may need different cleanup and therefore
> need to skip the standard cleanup. But if this is an issue I'm
> probably already using raise rather than break because it's most
> likely an error rather than a normal case.
>
> My guess is that it probably made a lot more sense before exceptions
> were added to Python.

I don't see what exceptions have to do with it. There are three
special ending cases: with break, empty sequence and sequence
exhausted. Empty sequence is the only one that isn't handled easily.

I suspect the use of 'else' was simply taking advantage of an
existing keyword, complete with contorted justification of why
that was acceptable.

What I'd like to see is a more general exit clause that also
applies to the while statement. Something like:

on break:
on normal_exit:
on empty_sequence:
on body_not_executed:

"on" would be a keyword, but the other four words would
only be recognized in the context of the "on" statement. It
requires one new keyword, but I think that it's easily
recognizable exactly what it does.

John Roth

>
>
> -- 
> Steve Horne
>
> steve at ninereeds dot fsnet dot co dot uk






More information about the Python-list mailing list