Live data from Hacker News

The optional “else” in Python loops

shahriar.svbtle.com

1–10 of 50 posts

Re: The optional “else” in Python loops

#3
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.

Re: The optional “else” in Python loops

#4
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

I agree. Intuitively, I would expect for item in list: .... else: ... to execute the "else" clause only if the list was empty and hence the for loop didn't iterate at all.

Re: The optional “else” in Python loops

#5
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.

If you're using break to signal success, that is how else acts.

Re: The optional “else” in Python loops

#6
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.

[deleted]

Re: The optional “else” in Python loops

#7
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

What finally made it intuitive for me: assuming the loop contains an if condition: break, you can consider the else-clause to be the else of the if statement within the loop.

Re: The optional “else” in Python loops

#8
post #5

Earlier quoted context omitted.

I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.

If you're using break to signal success, that is how else acts.

I've been using break to signal failure. I'll keep that in mind.

Re: The optional “else” in Python loops

#9
post #7
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

What finally made it intuitive for me: assuming the loop contains an if condition: break, you can consider the else-clause to be the else of the if statement within the loop.

That logic definitely helps me think about it. But it does seem like a switch from the other wording that python uses. Feels like finally would be more intuitive, whereas without thinking about the search loop case else feels like something that would be triggered by a break or return. I wonder if using the feature would be more common if it was more intuitively worded.

Re: The optional “else” in Python loops

#10
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

Raymond Hettinger has suggested that instead of `else:` the keyword should have been called `nobreak:` which conveys intentions better.
Post reply on HN