Generator (computer programming): Difference between revisions

Content deleted Content added
m removed essay-y wordage
m Python: PEP-8
Line 479:
 
# Example use: printing out the integers from 10 to 20.
# Note that this iteration terminates normally, despite
# countfrom() being written as an infinite loop.
 
Line 495:
p = []
while True:
# This works in Python 2.5+
if not any(n % f == 0 for f in
itertools.takewhile(lambda f: f*f <= n, p)):
yield n
p.append(n)
Line 512:
The following extends the example above by using a generator expression to compute squares from the countfrom generator function:
<syntaxhighlight lang="python">
squares = (n n* n for n in countfrom(2) )
 
for j in squares: