Generator (computer programming): Difference between revisions

Content deleted Content added
add linkt to continuation
generators are not like continuations (see the talk page)
Line 21:
</pre>
 
In Python, a generator can be thought of as an iterator that contains a [[continuation]] (a programming language structure which can be thought of as a "frozen [[stack frame]]"). Whenever the iterator's <code>next()</code> method is called, Python resumes the frozen frame, which executes normally until the next <code>yield</code> statement is reached. The generator's frame is then frozen again, and the yielded value is returned to the caller.
 
Because generators compute their yielded values only on demand, they are useful for representing sequences that are expensive to compute, or even infinite.