[Python-3000] else-clause on for-loops

Nicola Larosa nico at tekNico.net
Fri Mar 24 11:50:25 CET 2006


Steven Bethard wrote:
>> There was talk previously_ about removing the else clause on for-loops
>> (and while-loops).  One possibility would be to change the else-clause
>> to behave as expected above (i.e. only executed when the loop fails to
>> iterate over any items).

Fredrik Lundh:
> I'm well aware that Python 3000 doesn't aim to be backwards compatible,
> but I would prefer if we could refrain from subtly changing the behaviour
> of existing constructs in way that breaks all existing use, and makes it un-
> necessarily hard to convert old code (whether by hand or by machine).

That's right. Unfortunately this case does not present a clear balance of
advantages and disadvantages.


> (fwiw, the else statement in Python *always* means the same thing: exe-
> cute this when the controlling condition has been tested and found false)

Precisely. And that's why the current behavior is counterintuitive: "no
loops executed" is a better "false controlling condition" than "a few loops
executed, but not all", as is the case when using "break".

Even if it would be suboptimal, for backward compatibility's sake we could
keep the current "else" semantics, and adopt the "except" keyword for the
"no loops executed" case, as suggested by Greg Ewing.

Otherwise, I'd remove the current semantics altogether: they're confusing,
and apparently not useful enough.

-- 
Nicola Larosa - http://www.tekNico.net/

Life [...] is a tale told by an idiot, full of sound and fury,
signifying nothing. -- William Shakespeare, MacBeth
Life is like the chicken ladder: short and full of shit. -- Anonymous



More information about the Python-3000 mailing list