1
votes

Solved: It seems that the problem only occurs with PythonWin. I ran everything through the IDLE's python shell and it worked just fine. Must be a bug with PythonWin and not the code itself.

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

2
Can you post the whole error? Or more code. How are leftChild and rightChild generated? - jgritty
@JacobJCallahan don't post the info in comments, where you can't format it properly. Edit your question. - Mark Reed
From the code you provided, it seems that numList.extend() would always be receiving a list instance. I have a feeling this will only be solvable by seeing all of the code :-/ - jdi
numList.extend(list((self.Value,))) seems like it should be just numList.append(self.Value) ... (that's not your problem, but it would be less confusing) -- You didn't happen to shadow the builtin list did you? - mgilson
@jgritty: No that is wrong. That would append a nested list which is not what the OP would want. - jdi

2 Answers

0
votes

I would add a couple of prints to see what is the actual type when it gives the error, like:

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

A not so wild guess... Try instead of list((self.Value,)) to use [self.Value]. I have a feeling it will work... :-)

0
votes

There is nothing in your examples to indicate where the problem is coming from, but what it means is that somehow you are returning an object type and not an object instance. All I can offer at this point is to suggest another way to rework the genList() method and see if it magically fixes your issue.

You could try passing along the same result list through the recursion, instead of returning lots of temporary ones:

def genList(self, numList=None):
    if numList is None:
        numList = []

    if self.leftChild != 'none':
        self.leftChild.genList(numList)

    numList.append(self.Value)

    if self.rightChild != 'none':
        self.rightChild.genList(numList)

    return numList

results = rootNode.genList()

Also, is there a reason you are using 'none' instead of None ? I would just use a None instead of a string.

My suggested edits to your version are here: http://pastebin.com/FGf8Lcdu

And here is the output of your same interpreter code, under python3.3:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:>>> 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)
:<EOF>

In [2]: node4.genList()
Out[2]: [4]

In [3]: node1.genList()
Out[3]: [2, 4, 5, 13, 16]