I have a pdf in which I create page numbers for a table of content. Some topics in a pdf overlaps into multiple pages for that I need to use steps while others stay on one page.
I have created a custom iterator like this:
class IndexPageCounter:
"""
Used to create index page counter for a table of content
"""
def __iter__(self):
self.num = 1
return self
def __next__(self, step=1):
num = self.num
self.num += step
return num
and call it like this:
obj = iter(IndexPageCounter())
print(next(obj)) # this works fine
print(next(obj, step=2) # this doesn't work
# above line gives TypeError: next() takes no keyword arguments
I tried looking it up but I don't see any example of creating a custom iterator with step.
Edit:
I can't pass the value of step in the constructor as the value of the step is not constant.
nextdoesn't take any kwargs. Pass the step as a constructor argument (similar to howrangeworks), or use a different method.