Hacker News new | ask | show | jobs
by deckar01 2380 days 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.