I would like to create a Python class which should have a dynamic iterator function. That is, I would like to send the iterator function as a parameter instead of hardcoding it inside the class.
Here is an example class:
class fib:
def __init__(self, items, iter_function):
self.iter_function = iter_function
self.items = items
def __iter__(self):
self.iter_function(self.items)
with two example iterator functions:
def iter_func(items):
for item in items:
yield item
def iter_func2(items):
for item in items:
yield item*2
When I create an object with the following, I get an error:
a = fib([1,2,3],iter_func)
next(iter(a))
The error prints as:
----> 1 next(iter(a))
TypeError: iter() returned non-iterator of type 'NoneType'
Any ideas are appreciated.