[Python-ideas] for/else statements considered harmful

MRAB python at mrabarnett.plus.com
Thu Jun 7 02:15:18 CEST 2012

On 07/06/2012 00:20, Alice Bevan–McGregor wrote:
> Howdy!
> Was teaching a new user to Python the ropes a short while ago and ran
> into an interesting headspace problem: the for/else syntax fails the
> obviousness and consistency tests.  When used in an if/else block the
> conditional code is executed if the conditional passes, and the else
> block is executed if the conditional fails.  Compared to for loops
> where the for code is repeated and the else code executed if we
> "naturally fall off the loop".  (The new user's reaction was "why the
> hoek would I ever use for/else?")
I find the easiest way to think of it is imagine you're searching a
list. If you find what you're looking for you break, else you do
something else.

More information about the Python-ideas mailing list