The program below [Python 3.4] is a simple Eratoshenes sieve:
from itertools import * def excl(ns,pr): return (i for i in ns if i%pr) def sieve(ns): while True: pr=next(ns) yield pr ns=excl(ns,pr)
which produces [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]. OK. Uncommenting the line in which inline excl (), and commenting out the call, gives [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]. Why?
Is this related to the problems that arise when modifying a sequence within a loop that iterates over it?
Thanks for any hint.
source share