for: else: - any practical uses for the else clause?
MonkeeSage
MonkeeSage at gmail.com
Sat Sep 30 19:01:31 EDT 2006
BJörn Lindqvist wrote:
> The code that you write in the positions A and B really are misplaced.
> They arent part of the iteration of list. The two tasks, find item and
> do something with item should be separated.
I think it is useful to have them joined. Consider a contrived example:
for i in (1,2,3,0,5,6):
try:
print 10 / i
except:
print 'Error in data'
break
else:
print 'Data processed cleanly'
Yes, you could use a flag variable instead:
done = 1
for i in (1,2,3,0,5,6):
try:
print 10 / i
except:
print 'Error in data'
done = 0
break
if done:
print 'Data processed cleanly'
...but that is not as clean imo.
Regards,
Jordan
More information about the Python-list
mailing list