With a generator function, this is how itertools.count can be implemented (from documentation):
def count(start=0, step=1):
# count(10) --> 10 11 12 13 14 ...
# count(2.5, 0.5) -> 2.5 3.0 3.5 ...
n = start
while True:
yield n
n += step
I am trying to find how a similar iterator could be implemented without a generator function.
class Count:
def __init__(self, start=0, step=1):
self.c = start
self.step = step
def __iter__(self):
return self
def __next__(self):
n = self.c
self.c += self.step
return n
Is this the implementation?
For sure, it does not make any practical sense, the point is just to get an idea of how it is achieved with generators.