Inheritance issue...

M

MooMaster

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?
 
M

Matimus

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:
.... def __init__(self, a):
.... self.a = a
........ def __init__(self, a, b):
.... super(C, self).__init__(a)
.... self.b = b
....2

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

Matt
 
D

Diez B. Roggisch

MooMaster said:
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.

The last sentence contains the important bit. You need to use
new-style-classes, which means they have to have the ancestor "object"
somewhere in their inheritance-graph.

Like this:

class Foo(object): pass

Certainly one of the somewhat uglier corners of Python...

Diez
 
M

MooMaster

MooMaster said:
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.

The last sentence contains the important bit. You need to use
new-style-classes, which means they have to have the ancestor "object"
somewhere in their inheritance-graph.

Like this:

class Foo(object): pass

Certainly one of the somewhat uglier corners of Python...

Diez

Thanks guys, I hadn't even heard of the distinction between "old" and
"new" style classes...is this in the tutorial somewhere? I didn't see
it in Classes...
 
C

Carl Banks

MooMaster schrieb:
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.
The last sentence contains the important bit. You need to use
new-style-classes, which means they have to have the ancestor "object"
somewhere in their inheritance-graph.
Like this:
class Foo(object): pass
Certainly one of the somewhat uglier corners of Python...

Thanks guys, I hadn't even heard of the distinction between "old" and
"new" style classes...is this in the tutorial somewhere? I didn't see
it in Classes...

Here's some reading material (it can get deep):

http://www.python.org/doc/newstyle/
http://www.python.org/download/releases/2.2.3/descrintro/


New style classes have been a part of Python since 2.2, but
unfortunately many documents intended for newbies don't even mention
them, which seems to cause more confusion than it prevents, such as
when newbies go Googling for code examples and see incompatible usage
of new-style classes. For simple uses, new-style and old-style
classes behave similarly enough that it doesn't matter which you use,
but new-style classes support lots of things that old-style classes
don't, and usage of these features is getting more and more common.
Newbies who aren't being alerted to this are getting a raw deal.

Thankfully old-style classes are getting the axe in Python 3.0 and
hopefully this confusion will go away.

Until you switch to 3.0, inherit your base classes from object; it
will ensure that more recent additions like properties and super()
calls work.

class Node(object):
....


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top