Martin McBride
Aug 30, 2022

--

That is a slightly different justification to the one you gave in the article. As you say, using a generator (or even a generator comprehension) is very useful if you want to avoid storing all the results in memory. I totally agree.

In the article you talk about using a generator to simplify the code, which I think is slightly less justified. It imposes a small burden on the caller, that they get an iterator back rather than a list. I guess it depends on whether the codebase in general uses iterators, but if the codebase doesn't use iterators very much it might cause confusion.

An alternative would be to use a list comprehension to avoid the loop.

--

--

Martin McBride
Martin McBride

Responses (1)