Re:The optional “else” in Python loops
by thatswrong0 on Thursday July 17, 2014 @08:30PM
(#8051056)
I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.
[ Parent
| on HN ]
If you're using break to signal success, that is how else acts.
Re:The optional “else” in Python loops
by thatswrong0 on Thursday July 17, 2014 @08:41PM
(#8051102)
I've been using break to signal failure. I'll keep that in mind.
Re:The optional “else” in Python loops
by maxerickson on Thursday July 17, 2014 @08:45PM
(#8051113)
Just to illustrate:
>>> for item in (1,2):
print(item)
else:
print('Done!')
1
2
Done!
>>> for item in (1,2):
print(item)
break
else:
print('Done!')
1
>>>
edit: also: >>> for item in ():
print(item)
else:
print('Done')
Done