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

Gerald Britton gerald.britton at gmail.com
Thu Oct 8 15:08:51 CEST 2009

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:
   if something_happened_with(x):
else: # only get here if nothing happened
#end of for loop

since it visually ties the "nothing happened" to the "for"  There's a
logical context that one can see.  An equivalent without an else

#for loop without else clause
something_happened = False
for x in y:
   if something_happened_with(x):
     something_happened = True
# end of for loop.  We get here whether something happened or not

if not something_happened:
#end of logical context

lacks such visual clues (and is more verbose and introduces another
variable) and has the potential for future problems, since another
programmer may add something else between the end of the loop and the
conditional call to nothing_happened.  OTOH if said programmer put it
in the else clause, he or she would hopefully think twice if it
belonged there (which it might not).

If the syntax were changed (snowball's chance in hades, I bet!) it
would break existing programs;  if a warning were issued instead,
working programs would suddenly spout spurious messages.


On Thu, Oct 8, 2009 at 6:19 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Yuvgoog Greenle wrote:
>> So the only way to the "else" clause is useful is when a "break" can
>> skip it. If there is no "break", you might as well remove the "else" and
>> deindent.
> Yup.
> As to the original question in this thread - definitely possible, and
> shouldn't be particularly difficult.
> The AST and the symbol table generation don't really keep track of loop
> nesting (as they don't care all that much), so you would defer picking
> this situation up to the actual compilation stage (Python/compile.c in
> the source tree).
> Conveniently, the compiler already searches for a LOOP fblock in order
> to generate the syntax error for using break outside a loop, so adding a
> "fb_break" field to the fblock struct would be a simple way to keep
> track of whether or not a loop contains a break statement (although I
> suspect you would have to reverse the current fblock search order to
> find the innermost loop instead of the outermost one).
> Then in compiler_for and compiler_while, the fb_break field on the
> relevant LOOP fblock could be checked whenever an OrElse clause was
> present in the AST. A SyntaxWarning would then be emitted whenever the
> else clause was present but the break flag was not set on the relevant
> LOOP fblock.
> That's just an implementation sketch obviously, and not actually tested
> in any way shape or form. Still, it should be fairly straightforward to
> add the warning.
> Cheers,
> Nick.
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> ---------------------------------------------------------------
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

Gerald Britton

More information about the Python-ideas mailing list