Inheritance problem?

K

KraftDiner

I have a class

class MyClass(MyBaseClass)
def __init__(self)
super(self.__class__, self).__init__()
self.type = MyClassType
return self

It has a few methods...
I have another class and the only difference is the __init__ method..

I tried this:
class MySpecialClass(MyClass)
def __init__(self)
super(self.__class__, self).__init__()
self.type = MySpecialClassType # This is the only line
that's different between the two classes.
return self

At runtime I get an error:
RuntimeError: maximum recursion depth exceeded
What have I done wrong?
 
P

Pierre Barbier de Reuille

Well, I would even add : don't use super !
Just call the superclass method :

MyClass.__init__(self)



Simon Percivall a écrit :
 
X

Xavier Morel

Pierre said:
Well, I would even add : don't use super !
Just call the superclass method :

MyClass.__init__(self)



Simon Percivall a écrit :
Bad idea if you're using new-style classes with a complex inheritance
hierarchy and multiple inheritance.
 
P

Pierre Barbier de Reuille

Xavier Morel a écrit :
Bad idea if you're using new-style classes with a complex inheritance
hierarchy and multiple inheritance.

As a reference :

http://fuhm.org/super-harmful/

I may say this is the only place I ever saw what "super" *really* is
for. The behavior is far too complex and misthought. All I can say is :
don't use it ! It solves *nothing* and creates too many bugs in the long
run.
 
X

Xavier Morel

Pierre said:
Xavier Morel a écrit :

As a reference :

http://fuhm.org/super-harmful/

I may say this is the only place I ever saw what "super" *really* is
for. The behavior is far too complex and misthought. All I can say is :
don't use it ! It solves *nothing* and creates too many bugs in the long
run.

My own encounter with the subject was Guido's "Unifying types and
classes in Python 2.2" (http://www.python.org/2.2.3/descrintro.html#mro
for the part on super itself), but I'll keep your link close by.
 
M

Mike Meyer

Xavier Morel said:
Bad idea if you're using new-style classes with a complex inheritance
hierarchy and multiple inheritance.

To quote the original code:

class MyClass(MyBaseClass)
def __init__(self)
super(self.__class__, self).__init__()
self.type = MyClassType
return self

class MySpecialClass(MyClass)
def __init__(self)
super(self.__class__, self).__init__()
self.type = MySpecialClassType
return self

The only place it uses self.__class__ is in the calls to super. Super
finds the superclass of it's first argument. If that argument is
self.__class__, then super will always return the superclass of the
class of self, *not* the superclass of the class who's code is being
run. That's why the code resuls in an infinite recursion.

And a note to the OP: __init__'s return value is ignored. You should
delete the "return self" from your methods.

<mike
 
K

KraftDiner

So ok I've written a piece of code that demonstrates the problem.
Can you suggest how I change the Square class init?

class Shape(object):
def __init__(self):
print 'MyBaseClass __init__'

class Rectangle(Shape):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Rectangle
print 'Rectangle'

class Square(Rectangle):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Square
print 'Square'

r = Rectangle()
s = Square()
 
P

Pierre Barbier de Reuille

KraftDiner a écrit :
So ok I've written a piece of code that demonstrates the problem.
Can you suggest how I change the Square class init?

class Shape(object):
def __init__(self):
print 'MyBaseClass __init__'

class Rectangle(Shape):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Rectangle
print 'Rectangle'

class Square(Rectangle):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Square
print 'Square'

r = Rectangle()
s = Square()

I suggest you have a look at the link I gave before :
http://fuhm.org/super-harmful/

It gives a good explanation about what happens with "super".

At least, if you *really* want to use it, change your code like that :

class Shape(object):
def __init__(self):
super(Shape, self).__init__()
print 'Shape __init__'

class Rectangle(Shape):
def __init__(self):
super(Rectangle, self).__init__()
self.type = Rectangle
print 'Rectangle'

class Square(Rectangle):
def __init__(self):
super(Square, self).__init__()
self.type = Square
print "Square"

r = Rectangle()
s = Square()


But, once more, I would recommand to use direct method call ....

Pierre
 
S

Scott David Daniels

KraftDiner said:
So ok I've written a piece of code that demonstrates the problem.
Can you suggest how I change the Square class init?

class Shape(object):
def __init__(self):
print 'MyBaseClass __init__'

class Rectangle(Shape):
def __init__(self):
# super(self.__class__, self).__init__()
super(Rectangle, self).__init__() # XXX fixed
self.type = Rectangle
print 'Rectangle'

class Square(Rectangle):
def __init__(self):
# super(self.__class__, self).__init__()
super(Square, self).__init__() # XXX fixed
 

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

Forum statistics

Threads
474,275
Messages
2,571,378
Members
48,070
Latest member
nick_tyson

Latest Threads

Top