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

Jacob Holm jh at improva.dk
Fri Oct 9 08:24:47 CEST 2009


Steven D'Aprano wrote:
> On Thu, 8 Oct 2009 07:57:38 pm Stefan Rank wrote:
>> This would both help prevent wrong usage because of a false intuition
>> and, if adequately and prominently documented, would help those
>> interested in learning about for/else while/else.
>>
>> Easily doable? Maybe even make it a SyntaxError?
> 
> -1000 on making it a SyntaxError. SyntaxErrors are for things which are 
> WRONG, not for things which are unnecessary or redundant or pointless.
> 
> Should this be prohibited too?
> 
> for x in seq:
>     if 0: break
> else:
>     print "whatever"
> 


No, see below.


> 
> 
> What happens when the break is under a __debug__ test? The same source 
> compiles to two different byte-codes, one of which has break, the other 
> doesn't. 


If I understand Nick correctly, this is irrelevant.  The warning/error 
generation would be handled by an earlier stage than the optimization 
that removes the break.

  - Jacob



More information about the Python-ideas mailing list