[Python-ideas] for/else syntax

Bruce Leban bruce at leapyear.org
Fri Oct 2 05:33:18 CEST 2009

On Thu, Oct 1, 2009 at 8:11 PM, Yuvgoog Greenle <ubershmekel at gmail.com>wrote:

> Question #2: When would the code in the "else" block execute? If you don't
> know, it's ok to guess.
>     If they answer "when a break didn't occur", I'm willing to give you,
> the asker, up to 100 lines of patch code in whatever python project you
> want..
>     If they answer "maybe when the for loop didn't run or was an empty
> list" then you don't owe me anything.
When I see for/else I think of the second case even though I know it's not
that. I think that second pattern is very common. In fact, I wrote code
today that used it and I had to store the list in a variable and wrap the
for in an if to test whether it was empty. Arguably, either one of these is
appropriate for the word "else".

I'd rather something like:

    for x in y:
    else if no_break:
      more stuff
    else if no_elements:
      more stuff

OK. This is ugly, but it's clear which of these is which use case. Surely
someone can make this better in time for Python 4.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091001/29864b43/attachment.html>

More information about the Python-ideas mailing list