Python (programming language): Difference between revisions

Content deleted Content added
Less silly generator example
Line 244:
To use this function simply call, e.g.:
for i in generate_primes(100):
print i,You can now use the generator generate_ints:
 
The definition of a generator appears identical to that of a function, except the keyword <code>yield</code> is used in place of <code>return</code>. However, a generator is an object with persistent state, which can repeatedly enter and leave the same dynamic extent. A generator call can then be used in place of a list, or other structure whose elements will be iterated over. Whenever the <code>for</code>-loop in the example requires the next item, the generator is called, and yields the next item.