Generator (computer programming): Difference between revisions

Content deleted Content added
attempt to tighten the Python generator description
clearer statement of generators' use
Line 23:
In Python, a generator can be thought of as an iterator that contains 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.
Generators can be used to loop through the values of a list lazily. This is useful when only the first few items of the list are likely to be needed, and calculating the entire list would be costly or impractical.
 
==References==