<div dir="ltr"><div><div><div><br></div>I'm getting the error in the subject, from the following code:<br> def add(self, key):<br> """<br> Adds a node containing I{key} to the subtree<br>
rooted at I{self}, returning the added node.<br> """<br> node = self.find(key)<br> if not node:<br> node.key = key<br> # placeholder<br> node.left, node.right = self.__class__(parent=node), self.__class__(parent=node)<br>
return (False, node)<br> else:<br> if random.random() < 0.5:<br> print('node.left is %s' % node.left)<br> return BinaryTree.add(self=node.left, key=key)<br>
else:<br> print('node.right is %s' % node.left)<br> return BinaryTree.add(self=node.right, key=key)<br><br></div>The above add() method is part of a BinaryTree(object) class, whose subclass is RedBlackTree.<br>
<br></div><div>We need to explicitly call BinaryTree.add() with an explict self, to avoid inappropriately calling RedBlackTree.add().; BinaryTree.add() is being called with a RedBlackTree instance as self.<br><br></div><div>
The debugging print and traceback look like:<br>node.left is 0 -1 red<br>Traceback (most recent call last):<br> File "app_main.py", line 51, in run_toplevel<br> File "test-red_black_tree_mod", line 328, in <module><br>
test()<br> File "test-red_black_tree_mod", line 316, in test<br> all_good &= test_duplicates()<br> File "test-red_black_tree_mod", line 194, in test_duplicates<br> tree.add(value)<br> File "/home/dstromberg/src/home-svn/red-black-tree-mod/trunk/duncan/red_black_bag_mod.py", line 919, in add<br>
(replaced, node) = super(RedBlackTree, self).add(key=key)<br> File "/home/dstromberg/src/home-svn/red-black-tree-mod/trunk/duncan/red_black_bag_mod.py", line 376, in add<br> return BinaryTree.add(self=node.left, key=key)<br>
TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead)<br><br></div><div>Why is it complaining that .add() is getting nothing, when node.left isn't None? As you can see above the traceback, it's got a value represented by "node.left is 0 -1 red".<br>
<br></div><div>python 2.x, python 3.x and pypy all give this same error, though jython errors out at a different point in the same method.<br><br></div><div>Thanks!<br><br></div></div>