Python list recursion type error

Posted by Jacob J Callahan on Stack Overflow See other posts from Stack Overflow or by Jacob J Callahan
Published on 2012-12-01T02:46:13Z Indexed on 2012/12/01 5:04 UTC
Read the original article Hit count: 141

Filed under:
|
|

I can't seem to figure out why the following code is giving me a TypeError: 'type' object is not iterable

pastebin: http://pastebin.com/VFZYY4v0

def genList(self):
    #recursively generates a sorted list of child node values
    numList = []
    if self.leftChild != 'none':
        numList.extend(self.leftChild.genList())  #error
    numList.extend(list((self.Value,)))
    if self.rightChild != 'none':
        numList.extend(self.rightChild.genList()) #error
    return numList

code that adds child nodes (works correctly)

def addChild(self, child):
    #add a child node. working
    if child.Value < self.Value:
        if self.leftChild == 'none':
            self.leftChild = child
            child.parent = self
        else:
            self.leftChild.addChild(child)
    elif child.Value > self.Value:
        if self.rightChild == 'none':
            self.rightChild = child
            child.parent = self
        else:
            self.rightChild.addChild(child)

Any help would be appreciated.

Full interpreter session: >>> import BinTreeNode as BTN
>>> node1 = BTN.BinaryTreeNode(5)
>>> node2 = BTN.BinaryTreeNode(2)
>>> node3 = BTN.BinaryTreeNode(12)
>>> node3 = BTN.BinaryTreeNode(16)
>>> node4 = BTN.BinaryTreeNode(4)
>>> node5 = BTN.BinaryTreeNode(13)
>>> node1.addChild(node2)
>>> node1.addChild(node3)
>>> node1.addChild(node4)
>>> node1.addChild(node5)
>>> node4.genList()
<class 'list'>
>>> node1.genList()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:...\python\BinTreeNode.py", line 47, in genList
numList.extend(self.leftChild.genList()) #error
File "C:...\python\BinTreeNode.py", line 52, in genList
TypeError: 'type' object is not iterable

© Stack Overflow or respective owner

Related posts about python

Related posts about recursion