Dan Stromberg wrote: > I'm getting the error in the subject, from the following code: > def add(self, key): > """ > Adds a node containing I{key} to the subtree > rooted at I{self}, returning the added node. > """ > node = self.find(key) > if not node: > node.key = key > # placeholder > node.left, node.right = self.__class__(parent=node), > self.__class__(parent=node) > return (False, node) > else: > if random.random() < 0.5: > print('node.left is %s' % node.left) > return BinaryTree.add(self=node.left, key=key) > else: > print('node.right is %s' % node.left) > return BinaryTree.add(self=node.right, key=key) > > The above add() method is part of a BinaryTree(object) class, whose > subclass is RedBlackTree. > > 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. > > The debugging print and traceback look like: > node.left is 0 -1 red > Traceback (most recent call last): > File "app_main.py", line 51, in run_toplevel > File "test-red_black_tree_mod", line 328, in <module> > test() > File "test-red_black_tree_mod", line 316, in test > all_good &= test_duplicates() > File "test-red_black_tree_mod", line 194, in test_duplicates > tree.add(value) > File > "/home/dstromberg/src/home-svn/red-black-tree- mod/trunk/duncan/red_black_bag_mod.py", > line 919, in add > (replaced, node) = super(RedBlackTree, self).add(key=key) > File > "/home/dstromberg/src/home-svn/red-black-tree- mod/trunk/duncan/red_black_bag_mod.py", > line 376, in add > return BinaryTree.add(self=node.left, key=key) > TypeError: unbound method add() must be called with BinaryTree instance as > first argument (got nothing instead) > > 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". > > 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. > > Thanks!
I never ran into that, but apparently you cannot pass self as a keyword parameter: >>> class A(object): ... def add(self): pass ... >>> a = A() >>> A.add(a) >>> A.add(self=a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unbound method add() must be called with A instance as first argument (got nothing instead) -- http://mail.python.org/mailman/listinfo/python-list