Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45522 > unrolled thread

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

Started byDan Stromberg <drsalists@gmail.com>
First post2013-05-18 12:01 -0700
Last post2013-05-18 12:01 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) Dan Stromberg <drsalists@gmail.com> - 2013-05-18 12:01 -0700

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

FromDan Stromberg <drsalists@gmail.com>
Date2013-05-18 12:01 -0700
SubjectTypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead)
Message-ID<mailman.1812.1368903676.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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!

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web