There 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.
A Python Guide for the Ages
11–20 of 59 posts
Re: A Python Guide for the Ages
#12There 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…
Generator return, on the other hand, is something I didn’t even know about (basically, return X in a generator raises StopIteration(X)). I don’t think it’s super useful because even inspecting the arguments to an exception tends to be uncommon practice in most code I’ve seen (more frequently, you just want to report or suppress the error depending on its type). I’m sure there’s a niche use for it somewhere though!
Re: A Python Guide for the Ages
#13There 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 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.
Re: A Python Guide for the Ages
#14There 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've used both, for with else more often. It avoided some booleans and if's that would have been much less clear/easy to get wrong. I'd like a different name for for "for's" else. I've been using method/function redefinition in place of conditionals related to initialization.
>When used with a loop, the else clause has more in common with the else clause of a try statement than it does with that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs.
Re: A Python Guide for the Ages
#15I like this. For those of you for whom this list is too terse, check out the excellent LearnXinYminutes site at https://learnxinyminutes.com/docs/python/
Re: A Python Guide for the Ages
#16Re: A Python Guide for the Ages
#17The best cheatsheet which I have ever seen (besides maybe cheats.rs) is this Python cheatsheet by Laurent Pointal, absolutely outstanding in many ways: https://perso.limsi.fr/pointal/_media/python:cours:mementopy...
Re: A Python Guide for the Ages
#18Re: A Python Guide for the Ages
#19Re: A Python Guide for the Ages
#20There 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 find generators useful when working with very large data structures because generators can be pretty efficient. You can also use them to write little helper functions. Have a look at this package and its source [0]. Oh, and don't feel bad if you never have a need for generators! For the longest time I felt like a steal for rarely using classes but I am over it now :) 0. https://more-itertools.readthedocs.io/en/stab…
def my_generator():
yield 1
yield 2
return 3
If you use that generator in a for loop then it will only put 1 and 2 into the iterator variable. You have to use the generator in a more direct way to get access to the 3.I haven't made use of return values from generators in my code either, but I believe they're used under the hood in coroutines in async code.