Earlier quoted context omitted.
It’s consistent with try…else and if…else so I actually think it’s ok.
"else" usually means "instead of" or "otherwise" in those patterns. If the original case doesn't run, then the else case runs instead. In a for loop, the else clause only runs if the loop successfully completes (isn't broken), so the else in for-else means the total opposite of what it means in every other pattern. I think it would make a lot more sense if it were replaced with "done" or "upon" or something else that…
A Python Guide for the Ages
21–30 of 59 posts
Re: A Python Guide for the Ages
#22Re: A Python Guide for the Ages
#23There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.
I used it recently in some mutating code in which I wanted to make a change, and also know if it did actually make a change. If the routine gets to the end of the loop without finding a place to make a change, it hits the "else" and returns False.
Re: A Python Guide for the Ages
#24Re: A Python Guide for the Ages
#25There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.
I actually saw someone use that for-else pattern in advent of code earlier this month. I believe the consensus was that it was definitely the perfect choice for the given use case, but neither he nor I would ever dare use it in production because nobody knows about it and it's super unintuitive. I think the idea is cool (albeit rarely useful), but a different word than "else" for the same functionality would go a lon…
for ...:
...
else:
# loop ended, no positions found
return ...Re: A Python Guide for the Ages
#26 def f(xs = []):
xs.append(5)
return xs
print(f())
print(f())
will print: [5]
[5,5]
and I love python despite this horrendous decision, but it should be mentioned more often in beginner resources.edit: thanks for the downvote? I've answered on the order of dozens of questions about this very mechanic on SO, via IRC, and more... it's a well-known confusing factor
Re: A Python Guide for the Ages
#27Earlier quoted context omitted.
I prefer using the else clause to maintaining an "is_found" flag. Although this is lesser known syntax so I probably wouldn't use it when multiple developers are involved.
Yeah, the biggest risk with for-else is that some inexperienced dev comes along and misreads the else as belonging to an if statement inside the for (or, worse, attempting to “fix” it). I’ve seen this happen more than once…
It's actually a super useful pattern for any kind of search iteration. I've used it many times.
Re: A Python Guide for the Ages
#28There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.
for i in range(n):
if search(i) == val:
break
else:
raise KeyError("not found")
found_index = i
It reduces the need for an additional flag. More importantly it makes it easier to ensure that the break condition is satisfied such that the loop variable can be used properly later on. Personally, I think an else condition should almost always be there for loops that gets broken early, similarly to always finishing if else chain with a final else.ETA: Another pattern where it's really useful is to replace
while True:
with a safer guaranteed terminating loop, for i in range(MAX_ITER):
...
else:
raise RuntimeError("exceeded max iterations")Re: A Python Guide for the Ages
#29does anyone have recommendations for a good ruby cheatsheet?
Re: A Python Guide for the Ages
#30Does someone know similar guide for javascript/node.js or its a good thing to start one?