Here’s a minimal example for a custom class that is iterable similar to a generator:
class MyIterable(object): def __init__(self): pass # ... Your code goes here! def __iter__(self): # Usually there is no need to modify this! return self def __next__(self): # To indicate no further values, use: raise StopIteration return "test" # Return the next value here. Return n
Usage example:
it = MyIterable() print(next(it)) # Prints "test" print(next(it)) # Prints "test"
In case you want to read more about how to create your own iterators, I recommend this tutorial.