Inheritance issue...

Matimus mccredie at gmail.com
Mon Mar 3 13:47:09 EST 2008


On Mar 3, 9:37 am, MooMaster <ntv1... at gmail.com> wrote:
> 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?


Node should inherit from `object'. Unless you inherit from object you
are using old-style classes, which do not derive from type and cannot
use the super method.

Example:
class Node(object):

Also, __init__ does not return anything, ever. This doesn't make
sense:
>         self.root = super(Tree, self).__init__(val)

When you call the __init__ method of a base class, it will operate on
self.

Example:

>>> class CBase(object):
...     def __init__(self, a):
...         self.a = a
...
>>> class C(CBase):
...     def __init__(self, a, b):
...         super(C, self).__init__(a)
...         self.b = b
...
>>> c = C(1,2)
>>> c.a
1
>>> c.b
2

Doing things like self.__init__(val) doesn't make sense.

Matt



More information about the Python-list mailing list