Inheritance issue...

MooMaster ntv1534 at gmail.com
Mon Mar 3 12:37:21 EST 2008


I'm trying to use inheritance to create a simple binary tree, but it's
not going so well... here's what I pull from the documentation for
super()
"super( type[, object-or-type])

Return the superclass of type. If the second argument is omitted the
super object returned is unbound. If the second argument is an object,
isinstance(obj, type) must be true. If the second argument is a type,
issubclass(type2, type) must be true. super() only works for new-style
classes.
A typical use for calling a cooperative superclass method is:

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)
"

So here's what I do:

class Node:
    def __init__(self, val=0, prnt = None):
        self.value = val
        self.parent = prnt

class Tree(Node):
    def __init__(self, val=0):
        self.root = super(Tree, self).__init__(val)
        self.leftChild = None
        self.rightChild = None

    def addChild(self, value):
        if self.root == None:
            self.__init__(value)
        else:
            n = self.root
            while(n is not None):
                if(n.leftChild == None and n.rightChild == None):
                    n.leftChild = Node(value, n)
                elif(n.rightChild == None):
                    n.rightChild = Node(value, n)
                else:
                    if(n.leftChild.leftChild is not None and
n.leftChild.rightChild is not None):
                        n = n.rightChild
                    else:
                        n = n.leftChild

    def printTree(self):
        if self.root == None:
            print "None"
        else:
            n = self.root
            print n.value
            while(n is not None):
                if(n.leftChild is None):
                    print str(n.value) + "'s left child is None"
                elif(n.rightChild is None):
                    print str(n.value) + "'s right child is None"
                else:
                    if(n.leftChild.leftChild is not None and
n.leftChild.rightChild is not None):
                        n = n.rightChild
                    else:
                        n = n.leftChild
def main():
    play = Tree(1)
    play.addChild(2)
    play.addChild(3)
    play.addChild(4)
    play.addChild(5)
    play.printTree()

if __name__ == "__main__":
    main()



...and here's what I get:

Traceback (most recent call last):
  File "C:/Users/The_N_Channel/Desktop/funWithTrees.py", line 53, in
<module>
    main()
  File "C:/Users/The_N_Channel/Desktop/funWithTrees.py", line 45, in
main
    play = Tree(1)
  File "C:/Users/The_N_Channel/Desktop/funWithTrees.py", line 8, in
__init__
    self.root = super(Tree, self).__init__(val)
TypeError: super() argument 1 must be type, not classobj

Looks to me like the super(Tree, self)__init__(val) follows the
example in the documentation, but I may be a witch. Anyone know why
this doesn't work?



More information about the Python-list mailing list