Generator (computer programming): Difference between revisions

Content deleted Content added
Ajk (talk | contribs)
generators in general (requires copyediting)
Fredrik (talk | contribs)
m copy-friendly code
Line 11:
An example Python generator:
 
<pre>
def sums(n, min=1):
if n == 0:
if n == 0:
yield []
elif n >= min:
for i in range(min, n+1):
for sol in sums(n-min, i):
yield [i] + sol
</pre>
 
In Python, a generator can be thought of as an iterator that contains a frozen function call. Whenever the iterator's <code>next()</code> method is called, the frozen function call resumes where it was left off and runs until it reaches a <code>yield</code> statement. Then the function call is frozen again, the iterator spits out the yielded value, and execution continues with the iterator's caller.