def _odd_iter(): n = 1 while True: n = n + 2 yield n
def _not_divisible(n): return lambda x: x % n > 0
def primes(): yield 2 it = _odd_iter() while True: n = next(it) yield n it = filter(_not_divisible(n), it) # 这里不懂:filter()不是把_not_divisible(n)返回的函数依次作用于it的每个元素上吗? it不是一个无限序列吗?filter()是什么时候执行结束的??
for n in primes(): if n < 1000: print(n) else: break