[Python-ideas] for/except/else syntax
Steven D'Aprano
steve at pearwood.info
Fri Oct 9 05:17:06 CEST 2009
On Fri, 9 Oct 2009 11:09:18 am Yuvgoog Greenle wrote:
> The "else" in "for...break...else" is very simply a condition tested
> after the loop finished (exactly like any other "if" that would
> immediately follow the loop). The condition that's tested is simple:
> "if there was a break - skip this".
That is completely incorrect. That's not what the code does. break is an
unconditional jump to the end of the loop block, which includes the
else suite. The following Python code:
for x in seq:
print "ham"
if x: break
else:
print "spam"
print "done"
compiles to something like the following pseudo-code:
10: x = next item in seq
20: print "ham"
30: if x: GOTO 60
40: GOTO 10
50: print "spam"
60: print "done"
The only conditional test is the test "if x".
--
Steven D'Aprano
More information about the Python-ideas
mailing list