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) # this works
# it = filter(lambda x : x % n > 0, it) this doesn't work
for n in primes():
if n < 10:
print(n)
else:
break
它是一个提供素数的发电机。它工作正常,但如果在过滤器中直接使用lambda表达式,它就不起作用并打印数字9。 那为什么呢?