Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is not a good time to use python generators?
    primarykey
    data
    text
    <p>This is rather the inverse of <a href="https://stackoverflow.com/questions/102535/what-can-you-use-python-generator-functions-for">What can you use Python generator functions for?</a>: python generators, generator expressions, and the <code>itertools</code> module are some of my favorite features of python these days. They're especially useful when setting up chains of operations to perform on a big pile of data--I often use them when processing DSV files.</p> <p><strong>So when is it <em>not</em> a good time to use a generator, or a generator expression, or an <code>itertools</code> function?</strong></p> <ul> <li>When should I prefer <code>zip()</code> over <code>itertools.izip()</code>, or</li> <li><code>range()</code> over <code>xrange()</code>, or</li> <li><code>[x for x in foo]</code> over <code>(x for x in foo)</code>?</li> </ul> <p>Obviously, we eventually need to "resolve" a generator into actual data, usually by creating a list or iterating over it with a non-generator loop. Sometimes we just need to know the length. This isn't what I'm asking.</p> <p>We use generators so that we're not assigning new lists into memory for interim data. This especially makes sense for large datasets. Does it make sense for small datasets too? Is there a noticeable memory/cpu trade-off?</p> <p>I'm especially interested if anyone has done some profiling on this, in light of the eye-opening discussion of <a href="http://www.gossamer-threads.com/lists/python/python/76267" rel="noreferrer">list comprehension performance vs. map() and filter()</a>. (<a href="https://mail.python.org/pipermail//python-list/2001-December/096256.html" rel="noreferrer">alt link</a>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload