Yield on recursive data structure
<p>I'm trying to use a generator with a Python class that works somewhat similarly to a linked list. </p>

<p>Here is a really simple example of what I mean:</p>

<pre><code>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
</code></pre>

<p>Of course this is just an example, but it's enough to illustrate the point.</p>

<p>Now, I was expecting to be able to invoke something like:</p>

<pre><code>g = GeneratorTest([1,2,3,4,5])
for x in g:
    print x
</code></pre>

<p>And have the cycle stop when it reached the last value, but the for loop just continues endlessly.</p>

<p>I'm quite new to generators, so I'm sure it's a basic premise I'm missing here.</p>

<p>Is the problem related to the fact that I yield the same object that creates the generator?
I'm sure that if I had an object with a list of GeneratorTest objects, I could return each of these objects quite simply, but I feel as there should be a way to make this work without a "wrapper" object.</p>

<p>What am I missing here?</p>
