Inheritance problem?
Pierre Barbier de Reuille
p.barbierdereuille at free.fr
Fri Jan 6 14:56:10 EST 2006
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
More information about the Python-list
mailing list