[Python-ideas] for/else syntax
masklinn at masklinn.net
Sat Oct 3 13:44:11 CEST 2009
On 3 Oct 2009, at 08:15 , Jared Grubb wrote:
> On 2 Oct 2009, at 22:08, Nick Coghlan wrote:
>> After the additional posts in the discussion, I'm back to my previous
>> opinion - the construct should be referred to as break/else (or
>> for/break/else and while/break/else) and we should at least
>> having the compiler itself raise at least a SyntaxWarning if it
>> encounters a for/else or while/else construct without also
>> a break statement inside the loop.
> I would disagree. Imagine, while trying to debug code, you comment
> out an if statement inside the for loop, and now suddenly the whole
> loop becomes syntactically invalid. Plus, it's not only a break that
> could potentially make a for/else a valid construct.
I'm sure that's why Nick suggests a Warning, not an Error. There's no
reason to make a break-less (loop) else invalid, but there are plenty
of reasons to warn the programmer of such a pattern, as it's usually
either pointless or erroneous.
> Return and yield are both legitimate, and technically anything that
> can throw an exception is too.
Yes, but these are irrelevant to the for:else: or while:else: cases.
More information about the Python-ideas