Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead)

189 views
Skip to first unread message

Dan Stromberg

unread,
May 18, 2013, 3:01:12 PM5/18/13
to Python List

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!

Peter Otten

unread,
May 18, 2013, 3:24:46 PM5/18/13
to pytho...@python.org
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)


Peter Otten

unread,
May 18, 2013, 3:46:38 PM5/18/13
to pytho...@python.org
Dan Stromberg wrote:

> 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.

By the way, 3.x doesn't have unbound methods, so that should work.

Terry Jan Reedy

unread,
May 18, 2013, 5:05:57 PM5/18/13
to pytho...@python.org
It does for this example (3.3.1)
>>> c = C()
>>> c.m()
<__main__.C object at 0x00000000033FC5F8>
>>> C.m(c)
<__main__.C object at 0x00000000033FC5F8>
>>> C.m(self=c)
<__main__.C object at 0x00000000033FC5F8>



0 new messages