I am trying to use a generator with a Python class that works in much the same way as a linked list.
Here is a really simple example of what I mean:
class GeneratorTest(): def __init__(self, list): if list: self.elem = list[0] if list[1:]: self.n = GeneratorTest(list[1:]) else: self.n = None def __iter__(self): return self def next(self): my_next = self while my_next is not None: yield my_next my_next = my_next.n
Of course, this is just an example, but this is enough to illustrate this point.
Now I was expecting to be able to call something like:
g = GeneratorTest([1,2,3,4,5]) for x in g: print x
And stop the loop when it reaches the last value, but the for loop just continues indefinitely.
I am completely new to generators, so I am sure that this is the main premise that I am missing here.
Is the problem related to the fact that I give the same object that the generator creates? I am sure that if I had an object with a list of GeneratorTest objects, I could just return each of these objects, but I feel there must be a way to do this work without a wrapper object.
What am I missing here?
source share