I am trying to understand how the primes iterator works, the code was taken from a lecture.
I searched for count() but found only methods for lists, objects, I just do not understand how the line self._candidates = count(1) works and what it means.
where is the object we are trying to count 1 in? and the further use of it self._candidates.next() is also very confusing.
I mainly code in java albeit know basic python.
here is the code:
class Primes(object):
def __init__(self):
self._candidates = count(1)
def __iter__(self): return self
def next(self):
item = self._candidates.next()
if item > 1:
self._candidates = FilterMultiplies(self._candidates, item)
return item
class FilterMultiplies(object):def __init__(self, seq, n):
self._seq = iter(seq)
self._n = n
def __iter__(self): return self
def next(self):
item = self._seq.next()
while item % self._n == 0:
item = self._seq.next()
return item