for what are for/while else clauses

Alexander Schmolck a.schmolck at gmx.net
Mon Nov 17 07:03:44 EST 2003


Alex Martelli <aleax at aleax.it> writes:
> Python could presumably help a little by warning about an 'else' on
> a for or while loop that contains no 'break' statements.  But the
> reason Python's for/else and while/else statements are not intuitive
> to most people can be boiled down to identifying that "controlling
> condition" -- the fact that the 'controlling condition' is "a break
> statement has executed" is """hardly obvious or most particularly "the 
> only obvious" interpretation""", to repeat myself:-).

Hmm, I can't see the break here:

>>> for x in []: print 'something'
... else: print 'nothing'
...
nothing

(Not that I wouldn't argue that the semantics of else in loops are blindingly
obvious, but I can see the (albeit slightly strained) analogy with if/else).

 'as




More information about the Python-list mailing list