[Python-ideas] for/else syntax
Oleg Broytman
phd at phd.pp.ru
Fri Oct 2 15:57:25 CEST 2009
On Fri, Oct 02, 2009 at 09:46:38AM -0400, Gerald Britton wrote:
> for i, j in enumerate(something):
> # suite
> i += 1
> else:
> i = 0
>
> # i == number of things processed
>
> The thing here is, "i" won't be set by the "for" statement if
> "something" is empty. OTOH, if "something" is non-empty, i >= 1 at
> the end of the for-loop.
Wrong, 'i' will always be 0 whether 'something' is empty or not. Without
a 'break', 'else' is always executed.
Oleg.
--
Oleg Broytman http://phd.pp.ru/ phd at phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-ideas
mailing list