Can generatorizing a list improve Python performance?
<pre><code>def numbers(mi, ma):
    return [n for n in range(mi, ma + 1)]

def gen(xs):
    return (x for x in xs)

example = gen(numbers(10, 20))
</code></pre>

<p>In this example, can <code>gen</code> improve iteration performance of <code>numbers</code>? Why (not)?</p>

<pre><code>def numbersGen(mi, ma):
    return gen([n for n in range(mi, ma + 1)]) # Generator from list comprehension?
</code></pre>

<p>Can Python get as lazy as Haskell?</p>

<p>In this particular case, <code>range</code> is already about as lazy as you can hope for. There's no reason to further wrap its output in layers of indirection. Of course, the usual caveats of Python generators vs. Haskell laziness apply; namely, iterating through multiple times, or continuing from a specific point multiple times, is painful in Python, but the memory usage is generally more predictable as a result.</p>

python performance python-3.x generator