When I change to use lambda in filter(), the result of code changes. Why?
Code to calculate primes. You can copy the code to run it directly.
def get_odd_iter():
oddNum = 1
while True:
oddNum = oddNum + 2
yield oddNum
def not_divisible(n):
return lambda x: x%n>0
def prime_iter():
yield 2
odd_iter = get_odd_iter()
while True:
odd = next(odd_iter)
yield odd
# odd_iter = filter(not_divisible(odd), odd_iter) # <--(1)
odd_iter = filter((lambda x: x%odd>0) , odd_iter) # <--(2)
p = prime_iter()
print(next(p))
print(next(p))
print(next(p))
print(next(p))
print(next(p))
print(next(p))
When I use (1), everything goes well. When I change to (2), 9 will be in the result, which is actually not a prime number.