Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'explicitly': 0.05; '"""': 0.07; 'class,': 0.07; 'debugging': 0.07; 'method.': 0.07; 'pypy': 0.07; '51,': 0.09; 'dan': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:instance': 0.09; 'subject:method': 0.09; 'python': 0.11; 'def': 0.12; "%s'": 0.16; '(false,': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'add(self,': 0.16; 'complaining': 0.16; 'key):': 0.16; 'node)': 0.16; 'node.': 0.16; 'parameter:': 0.16; 'placeholder': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclass': 0.16; 'typeerror:': 0.16; 'unbound': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'adds': 0.24; 'skip:" 40': 0.26; 'pass': 0.26; 'code:': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'errors': 0.30; "i'm": 0.30; 'getting': 0.31; '"",': 0.31; '3.x': 0.31; 'apparently': 0.31; 'node': 0.31; 'file': 0.32; 'class': 0.32; 'skip:m 30': 0.32; 'thanks!': 0.32; '(most': 0.33; 'skip:b 30': 0.33; 'subject: (': 0.35; 'subject:with': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'keyword': 0.36; 'returning': 0.36; 'method': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'first': 0.61; 'different': 0.65; 'containing': 0.69; 'subject:nothing': 0.84; 'subject:add': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) Date: Sat, 18 May 2013 21:24:46 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849186.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368905100 news.xs4all.nl 15918 [2001:888:2000:d::a6]:50591 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45524 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 > 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 "", line 1, in TypeError: unbound method add() must be called with A instance as first argument (got nothing instead)