How this Python generators based inorder traversal method works
<p>I am quite new to python and still exploring. Came across generators and below code snippet implementing inorder binary tree traversal using generators:</p>

<pre><code>def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x

        yield t.label

        for x in inorder(t.right):
            yield x
</code></pre>

<p>Now I know following fact about generators: they remember the local variable values across calls. However this function is recursive. So how it remembers local variable values across these different recursive calls? </p>

<p>Also it was easy to understand normal recursive inorder program (not involving generators) as there were clear recursion termination conditions explicitly specified. But how this recursion with generators works?</p>

<p><code>inorder</code> returns a generator. <em>That</em> object is what remembers its local state between calls to <code>next</code>. There is no overlap between generators created by separate calls to <code>inorder</code>, even when called recursively.</p>

python