[Python-ideas] SyntaxWarning for for/while/else without break or return?

Masklinn masklinn at masklinn.net
Thu Oct 8 15:15:10 CEST 2009


On 8 Oct 2009, at 15:08 , Gerald Britton wrote:
> I'm still pretty much against the whole idea of changing this syntax
> or issuing warnings.  I like writing:
>
> # for loop using else clause
> for x in y:
>   do_something_with(x)
>   if something_happened_with(x):
>     break
> else: # only get here if nothing happened
>   nothing_happened()
> #end of for loop
>
[snip]
>
> if a warning were issued instead,
> working programs would suddenly spout spurious messages.

None of the proposals (as far as I can see) would emit a warning on  
this snippet: there is a `break` going with the `else:` clause, which  
is (as far as I can say from the discussion) the normal use case for  
`for: else:`.

The SyntaxWarning proposed would only be emitted on a `for: else:`  
*without* a break as it's entirely equivalent to just deleting the  
`else:` clause and dedenting the code it contains.

As far as outputting "spurious" messages in working programs, that's  
the point of a warning isn't it? Tell the programmer that something  
works, but might not do what one would like (or does nothing at all).



More information about the Python-ideas mailing list