0
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

It's a generator to provide prime numbers. It works fine but if a lambda expression is used directly in the filter, it just does not work and number 9 is printed. so why is that?

He Kunxi
  • 9
  • 2

0 Answers0