title: Convert recursive generator object to list
body: <p>I have been trying to implement a simple tree structure in Python. A tree begins at a single "root" node which has children, each of its children may have own children and so forth. </p>
  
  <p>Now, I want to print the names of all nodes of the tree, that is I want to convert it to a list. I sought to employ recursiveness but unfortunately yielding recursively returns a sub-tree of generator objects which I cannot convert to nodes.</p>
  
  <p>Could somebody help me and point out what I am doing wrong here please?</p>
  
  <pre><code>class Node:
  
    def __init__(self,name):
      self.name = name
      self.children = []
      self.parent = None
  
  
    def appendChild(self,child):
      self.children.append(child)
      if child is not None:
        child.parent = self
  
  
    def listChildren(self):
      yield self
      for child in self.children:
        yield child.listChildren()
      raise StopIteration
  
  # test
  r = Node("root")
  
  n = Node("name")
  r.appendChild(n)
  n.appendChild(Node("name2"))
  n.appendChild(Node("name3"))
  
  c = Node("child")
  n.appendChild(c)
  c.appendChild(Node("child2"))
  c.appendChild(Node("child3"))
  
  r.appendChild(Node("name4"))
  r.appendChild(Node("name5"))
  r.appendChild(Node("name6"))
  
  for child in r.listChildren():
      print child.name
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/User/Documents/TreeNode.py", line 40, in &lt;module&gt;
      print child.name
  AttributeError: 'generator' object has no attribute 'name'
  </code></pre>
  
  <p>A generator is supposed to be called when it is being iterated over, but in my case every child in r.listChildren() is, in turn, a generator object. If this is a design flaw, then I would have to look for another way of generating a list of node names.</p>
  
  <p>Thank you in advance!</p>