for ... else ?
paddy3118 at googlemail.com
Tue Jun 12 23:35:52 CEST 2007
On Jun 12, 7:55 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> En Tue, 12 Jun 2007 10:41:28 -0300, Chris Mellon <arka... at gmail.com>
> > On 6/12/07, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
> >> for x in iterable:
> >> do something with x
> >> else:
> >> do something when there are no more x
> >> You can think the above as:
> >> while there are still values in iterable:
> >> do something with the next value
> >> else:
> >> do something when there are no more items
> > This is a good way of phrasing it and I hope I can remember it,
> > because for..else always gives me trouble. To me, "else" indicates the
> > negative condition and I intuitively associate it with executing the
> > loop early, not normal exit. Personally, I think a different keyword
> > (maybe "after"?) would have done a better job of clarifying this.
> Yes, maybe, but it's hard to find a keyword equally applicable to "for"
> and "while" and creating two new keywords for essencially the same thing
> would be too much... Anyway it's too late to be changed now.
> Gabriel Genellina
Would replacing the word 'else' with 'then' read better? The implied
meaning is if the loop terminates normally *then* also do this block.
More information about the Python-list