Skip to content Skip to sidebar Skip to footer

Weird Behavior Of Python Iterator

I was playing around with Python generators and the itertools module and tried making an infinite version of the Sieve of Eratosthenes. Here is my code: from itertools import count

Solution 1:

You've fallen prey to a very common pitfall when using closures in Python: closures carry their scope, and you keep replacing the value in the same scope.

candidates = ifilter(lambda n, prime=prime: n % prime, candidates)

Post a Comment for "Weird Behavior Of Python Iterator"