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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to