[Python-ideas] for/else syntax

Steven D'Aprano steve at pearwood.info
Sat Oct 3 19:06:17 CEST 2009

On Sat, 3 Oct 2009 11:30:55 pm Yuvgoog Greenle wrote:

> if not break:
> Notice how readable and beautiful the code has become.

No I don't. It looks like you're testing against a boolean flag 
called "break".

> We have 2 implementation options:
> 1. an "if" that comes immediately after a for/while can have the
> special keywords "not" or "break".
> 2. the for/while syntax has an optional "if break" or "if not break".

Or #3: don't change a thing.

> I'd like to hear if people like this idea and if so, which of the 2
> options do you like better.

No. Neither of them.

> Check it out, there's no need for a syntax error 

That's right.

> because it's so obviously wrong:

Your reasoning is backwards. If it were obviously wrong, there *would* 
be need to have a syntax error:

>>> 4+*3
  File "<stdin>", line 1
SyntaxError: invalid syntax

Syntax errors are for code which can't be compiled, not for code that 
does something unexpected, or unintuitive, or pointless.

Steven D'Aprano

More information about the Python-ideas mailing list