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
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:
Also, is there a reason you are using
'none'instead ofNone? I would just use aNoneinstead 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: