title: How do Python Generators Save Memory
body: <p>I understand the general idea that a generator returns an iterable that 'saves state' and doesn't calculate everything at once, rather it calculates with each call to <code>next</code>.  How does this work?  For example <code>[x for x in range(10) if x%2==0]</code> vs <code>(x for x in range(10) if x%2==0)</code>.  In the list comprehension, everything is calculated and stored in memory at once.  </p>
  
  <p>In the generator, the entire list is not produced but instead an iterable generator object which calculates with each call to next.  But this generator must somehow know it's "bounds", right?  How does the generator know, if it isn't in the background performing all the calculations, where to pick up where it left off? I would think it has to know each step in the list comprehension and ultimately if you end up cycling through the entire generator until a StopIteration is hit, I would think you are using roughly the same amount of memory. </p>