Inheritance problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KraftDiner

    #1

    Inheritance problem?

    I have a class

    class MyClass(MyBaseC lass)
    def __init__(self)
    super(self.__cl ass__, 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.__cl ass__, self).__init__( )
    self.type = MySpecialClassT ype # 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?

  • Simon Percivall

    #2
    Re: Inheritance problem?

    Don't use self.__class__, use the name of the class.

    Comment

    • Pierre Barbier de Reuille

      #3
      Re: Inheritance problem?

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

      MyClass.__init_ _(self)



      Simon Percivall a écrit :[color=blue]
      > Don't use self.__class__, use the name of the class.
      >[/color]

      Comment

      • Xavier Morel

        #4
        Re: Inheritance problem?

        Pierre Barbier de Reuille wrote:[color=blue]
        > Well, I would even add : don't use super !
        > Just call the superclass method :
        >
        > MyClass.__init_ _(self)
        >
        >
        >
        > Simon Percivall a écrit :[color=green]
        >> Don't use self.__class__, use the name of the class.
        >>[/color][/color]
        Bad idea if you're using new-style classes with a complex inheritance
        hierarchy and multiple inheritance.

        Comment

        • Pierre Barbier de Reuille

          #5
          Re: Inheritance problem?

          Xavier Morel a écrit :[color=blue]
          > Pierre Barbier de Reuille wrote:
          >[color=green]
          >> Well, I would even add : don't use super !
          >> Just call the superclass method :
          >>
          >> MyClass.__init_ _(self)
          >>
          >>
          >>
          >> Simon Percivall a écrit :
          >>[color=darkred]
          >>> Don't use self.__class__, use the name of the class.
          >>>[/color][/color]
          > Bad idea if you're using new-style classes with a complex inheritance
          > hierarchy and multiple inheritance.[/color]

          As a reference :



          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.

          Comment

          • Xavier Morel

            #6
            Re: Inheritance problem?

            Pierre Barbier de Reuille wrote:[color=blue]
            > Xavier Morel a écrit :[color=green]
            >> Pierre Barbier de Reuille wrote:
            >>[color=darkred]
            >>> Well, I would even add : don't use super !
            >>> Just call the superclass method :
            >>>
            >>> MyClass.__init_ _(self)
            >>>
            >>>
            >>>
            >>> Simon Percivall a écrit :
            >>>
            >>>> Don't use self.__class__, use the name of the class.
            >>>>[/color]
            >> Bad idea if you're using new-style classes with a complex inheritance
            >> hierarchy and multiple inheritance.[/color]
            >
            > 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.[/color]

            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.

            Comment

            • Mike Meyer

              #7
              Re: Inheritance problem?

              Xavier Morel <xavier.morel@m asklinn.net> writes:[color=blue]
              > Pierre Barbier de Reuille wrote:[color=green]
              >> Well, I would even add : don't use super !
              >> Just call the superclass method :
              >> MyClass.__init_ _(self)
              >> Simon Percivall a écrit :[color=darkred]
              >>> Don't use self.__class__, use the name of the class.[/color][/color]
              > Bad idea if you're using new-style classes with a complex inheritance
              > hierarchy and multiple inheritance.[/color]

              To quote the original code:

              class MyClass(MyBaseC lass)
              def __init__(self)
              super(self.__cl ass__, self).__init__( )
              self.type = MyClassType
              return self

              class MySpecialClass( MyClass)
              def __init__(self)
              super(self.__cl ass__, self).__init__( )
              self.type = MySpecialClassT ype
              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
              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

              Comment

              • KraftDiner

                #8
                Re: Inheritance problem?

                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.__cl ass__, self).__init__( )
                self.type = Rectangle
                print 'Rectangle'

                class Square(Rectangl e):
                def __init__(self):
                super(self.__cl ass__, self).__init__( )
                self.type = Square
                print 'Square'

                r = Rectangle()
                s = Square()

                Comment

                • Pierre Barbier de Reuille

                  #9
                  Re: Inheritance problem?

                  KraftDiner a écrit :[color=blue]
                  > 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.__cl ass__, self).__init__( )
                  > self.type = Rectangle
                  > print 'Rectangle'
                  >
                  > class Square(Rectangl e):
                  > def __init__(self):
                  > super(self.__cl ass__, self).__init__( )
                  > self.type = Square
                  > print 'Square'
                  >
                  > r = Rectangle()
                  > s = Square()
                  >[/color]

                  I suggest you have a look at the link I gave before :


                  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(Rectangl e):
                  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

                  Comment

                  • Scott David Daniels

                    #10
                    Re: Inheritance problem?

                    KraftDiner wrote:[color=blue]
                    > 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.__cl ass__, self).__init__( )[/color]
                    super(Rectangle , self).__init__( ) # XXX fixed[color=blue]
                    > self.type = Rectangle
                    > print 'Rectangle'
                    >
                    > class Square(Rectangl e):
                    > def __init__(self):
                    > # super(self.__cl ass__, self).__init__( )[/color]
                    super(Square, self).__init__( ) # XXX fixed[color=blue]
                    > self.type = Square
                    > print 'Square'
                    >
                    > r = Rectangle()
                    > s = Square()
                    >[/color]


                    --
                    -Scott David Daniels
                    scott.daniels@a cm.org

                    Comment

                    Working...