3 ms·
I had my first legitimate use of for else last week after knowing about it for years and it (the keyword) never making much sense. I had to call an API for it
by hiccuphippo 8y ago
I had my first legitimate use of for else last week after knowing about it for years and it (the keyword) never making much sense.
I had to call an API for it to load data into a cache, but sometimes this wouldn't work so I made it try up to 3 times, otherwise it would show an error. So something like:
for i in range(3):
if load_cache():
log("success")
break
else:
log("failure")
Else made total sense here.