for-else

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Mar 4 19:02:38 EST 2008


bearophileHUGS at lycos.com writes:

> But then this article:
> http://tratt.net/laurie/tech_articles/articles/the_high_risk_of_novel_language_features
> has shown me that my problems with the 'else' of the 'for' mostly
> come from just its bad naming. The converge language is yet another
> very Python-like language, and it uses a better naming (the word
> "exhausted" is long and has a complex spelling for non-English
> speakers, so it's not perfect):
> 
> for ...:
>     ...
> exhausted:
>     ...
> broken:
>     ...

Rather than adding new keywords, I think the above would be better
spelled using the exception keywords::

    for foo in bar_sequence:
        # normal iteration
        spam(foo)
        if funky(foo):
            break
    except StopIteration, exc:
        # the iterator stopped normally
        eggs(exc)
    else:
        # the iterator exited abnormally, i.e. 'break'
        sausage()
    finally:
        # always executed, even on 'break'
        beans()

-- 
 \     “[T]he question of whether machines can think [...] is about as |
  `\         relevant as the question of whether submarines can swim.” |
_o__)                                              —Edsger W. Dijkstra |
Ben Finney



More information about the Python-list mailing list