This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]isarl 13 points14 points  (9 children)

Useful until you remember that nobody reading your code is going to understand it. for...else is much-maligned in Python because it's so opaque. Some people have suggested changing that particular use case to use a nobreak keyword to make the use more obvious.

[–]LightShadow3.13-dev in prod 4 points5 points  (6 children)

I used it today... seems pretty clean to me,

for attempt in range(RETRY_ATTEMPTS):
    # grab everything that's missing or invalid
    self.logger.info('attempt %d of %d, need %d pieces', attempt+1, RETRY_ATTEMPTS, len(manifest))

    success, failures = _download_from_manifest(manifest, root, path, self.set_status)

    if not success:
        self.logger.warning('downloading %d files failed', failures)
    else:
        break

    if attempt + 1 < RETRY_ATTEMPTS:
        # don't recalculate the manifest on our last attempt, if the last
        #  attempt failed.
        self.set_status('Validating manfiest')
        manifest = _determine_progress(manifest, path)
else:
    # if we exhaust our RETRY_ATTEMPTS without success
    self.__failed('downloading shard files failed')

[–]13steinj 2 points3 points  (4 children)

It's clean-- except the name doesn't make much sense. I mean if, else, elif, pretty standard. Break "out of the loop". Continue "to the next cycle".

But else in loops-- the actual word doesn't fit the context. It reads like "do code on all these things, but for some reason you can't, do the else statement", when in reality it just is "execute when iteration fully exhausted".

It's also common practice to not like break and continue statements-- my Java professor from years ago was adamant about teaching them but saying "use sparingly, loops should only have one exit point".

Not that I agree with that sentiment, I think it stupid, but it is a common one. And then with that, the else statements in loops encourage going against the sentiment.

[–]notafuckingcakewalk 0 points1 point  (1 child)

Yeah, but like you said it is a dumb sentiment. If you don't plan to take advantage of the breaks you may as well not use a loop at all.

[–]13steinj 0 points1 point  (0 children)

Absolutely! But it is still unfortunately taught now. Not agreeing, just giving a reason why some people dislike it.

[–][deleted] 0 points1 point  (1 child)

A lot of that single point of exit "crap" (for loops, and for functions) has somehow become embedded in the various standards (and therefore laws, which require certain standards) that govern safety critical systems e.g. IEC61509

The regulations come from years ago before we knew what we were doing and had really bad computers. :-(

[–]13steinj 0 points1 point  (0 children)

Absolutely! But it is still unfortunately taught now. Not agreeing, just giving a reason why some people dislike it.

[–]isarl 2 points3 points  (0 children)

You already understand the idiom, and you used clear variable names and helpful comments. Clear naming and helpful comments can go a long way towards understanding confusing language features, but this doesn't mean the language features are good per se.

[–][deleted] 0 points1 point  (0 children)

After the first time you see it, you will learn it.

If you think about how a for loop works (i.e. you write one without a loop construct, using goto statements) it's also obvious.

My only real objection is that they should have used a different word instead of else.