top | item 21755609

(no title)

gavanwoolery | 6 years ago

The author has done a really good overview here, however (perhaps by virtue of the example being too simple), the resulting code looks much less readable than the original code. Which I guess goes to say, there are probably situations where it is appropriate to use stuff like this and situations where it could be easily abused?

discuss

order

deckar01|6 years ago

I think of it like streams vs strings. When you are writing a one-off script, reading an entire file into a string in memory is fine. When you are writing a library and need to handle files in the wild, you are going to read the file in chunks and process them as a stream. Generators are like streams for iterables. You don't usually write code that manually builds streams. Likewise, you generally will not write generators that manually yield values. The standard library will provide a collection of tools that make the operations you were already performing on collections more memory efficient by using generators to avoid intermediate buffers.