for what are for/while else clauses

Diez B. Roggisch deets_noospaam at web.de
Fri Nov 14 13:31:08 EST 2003


Hi,

today I rummaged through the language spec to see whats in the for ... else:
for me. I was sort of disappointed to learn that the else clauses simply
gets executed after the loop-body - regardless of the loop beeing entered
or not.

So where is an actual use case for that feature? 

I imagined that the else-clause would only be executed if the loop body
wasn't entered, so I could write this

for r in result:
  print r
else:
  print "Nothing found, dude!"

instead of 

if len(result):
  for r in result
    print r
else:
  print "Nothing found, dude!"



waiting for enlightment,

Diez




More information about the Python-list mailing list