How to inheritance overwrite non virtual?

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

    How to inheritance overwrite non virtual?

    Hi!

    I've got an class with a couple of depending methods.
    I want to inheritance and create a new class where just
    one method ist overwritten.

    My problem: When python is executing the mother-method,
    not the depending mother-method is taken, it take the
    method of the chield.

    In my example, i want that K2.bestCaptionl ong() returns
    the same as KObject.bestCap tionlong() - no way!

    I've tryed (in K2) something like:
    def bestCaptionlong (self):
    return KObject().bestC aptionlong()
    which I expected to be like KObject::bestCa ptionlong() in
    C++, but it doesn't work ;-(

    Any Idea how to make the mother-class calling her method's?

    Here is my Example:

    class KObject:
    def __init__(self):
    self._label=Non e
    self._caption=N one
    self._captionlo ng=None
    def __str__(self):
    return (
    "label: [%s], %s, (%s)\n"+
    "caption: [%s], %s, (%s)\n"+
    "captionlon g: [%s], %s, (%s)"
    )%( self._label, self.label(), self.bestLabel( ),
    self._caption, self.caption(), self.bestCaptio n(),
    self._captionlo ng, self.captionlon g(), self.bestCaptio nlong()
    )
    def label(self):
    return self._label
    def caption(self):
    return self._caption
    def captionlong(sel f):
    return self._captionlo ng
    def bestLabel(self) :
    return self.label()
    def bestCaption(sel f):
    if self.caption() is not None:
    return self.caption()
    else:
    return self.bestLabel( )
    def bestCaptionlong (self):
    if self.captionlon g() is not None:
    return self.captionlon g()
    else:
    return self.bestCaptio n()

    class K2(KObject):
    def bestCaption(sel f):
    return "Overwritte n"

    k = KObject()
    k._label='x'

    k2 = K2()
    k2._label='x'

    print k
    print k2

    Thanks, AXEL.
  • Peter Otten

    #2
    Re: How to inheritance overwrite non virtual?

    Axel Straschil wrote:
    [color=blue]
    > I've tryed (in K2) something like:
    > def bestCaptionlong (self):
    > return KObject().bestC aptionlong()
    > which I expected to be like KObject::bestCa ptionlong() in
    > C++, but it doesn't work ;-([/color]

    The following will do:

    class Derived(Base):
    def method(self):
    return Base.method(sel f) # call base class method

    (Of course you need not override the method if the body of the derived class
    only calls the base class method.)

    Peter

    Comment

    • Axel Straschil

      #3
      Re: How to inheritance overwrite non virtual?

      Hi!
      [color=blue]
      > The following will do:
      >
      > class Derived(Base):
      > def method(self):
      > return Base.method(sel f) # call base class method[/color]

      I tried in my chield-class:
      class K2(KObject):
      def bestCaption(sel f):
      return "Overwritte n"
      def bestCaptionlong (self):
      return KObject.bestCap tionlong(self)

      The Problem ist inside of KObject.bestCap tionlong(self):
      return self.bestCaptio n()

      The mother-class is calling the method of the child-class, not
      it own method. I want the mother-method to be called like "the
      chield never was existing", but I find no way.

      Anny other idea?

      Thank, AXEL.

      Comment

      • anton muhin

        #4
        Re: How to inheritance overwrite non virtual?

        Axel Straschil wrote:
        [color=blue]
        > Hi!
        >
        >[color=green]
        >>The following will do:
        >>
        >>class Derived(Base):
        >> def method(self):
        >> return Base.method(sel f) # call base class method[/color]
        >
        >
        > I tried in my chield-class:
        > class K2(KObject):
        > def bestCaption(sel f):
        > return "Overwritte n"
        > def bestCaptionlong (self):
        > return KObject.bestCap tionlong(self)
        >
        > The Problem ist inside of KObject.bestCap tionlong(self):
        > return self.bestCaptio n()
        >
        > The mother-class is calling the method of the child-class, not
        > it own method. I want the mother-method to be called like "the
        > chield never was existing", but I find no way.
        >
        > Anny other idea?
        >
        > Thank, AXEL.[/color]

        Change
        return self.bestCaptio n()

        to
        return KObject.bestCap tion(self)

        hth,
        anton.

        Comment

        • Peter Otten

          #5
          Re: How to inheritance overwrite non virtual?

          Axel Straschil wrote:
          [color=blue]
          > Hi!
          >[color=green]
          >> The following will do:
          >>
          >> class Derived(Base):
          >> def method(self):
          >> return Base.method(sel f) # call base class method[/color]
          >
          > I tried in my chield-class:
          > class K2(KObject):
          > def bestCaption(sel f):
          > return "Overwritte n"
          > def bestCaptionlong (self):
          > return KObject.bestCap tionlong(self)
          >
          > The Problem ist inside of KObject.bestCap tionlong(self):
          > return self.bestCaptio n()
          >
          > The mother-class is calling the method of the child-class, not
          > it own method. I want the mother-method to be called like "the
          > chield never was existing", but I find no way.
          >
          > Anny other idea?
          >
          > Thank, AXEL.[/color]

          What you want seems to be

          class KObject:
          def bestCaptionlong (self):
          # class made explicit to guard against overriding
          return KObject.bestCap tion(self)

          Some additional remarks:
          Do-nothing accessor methods may be acceptable style in C++ but are
          superfluous in Python.
          The abundance of xxxCaption and label attributes somewhat obscures your
          design goal for me, but usually the ability to override a method to change
          its effect is a feature, not something to fight against.

          Peter




          Peter

          Comment

          • Richie Hindle

            #6
            Re: How to inheritance overwrite non virtual?


            [Axel][color=blue]
            > The mother-class is calling the method of the child-class, not
            > it own method. I want the mother-method to be called like "the
            > chield never was existing", but I find no way.[/color]

            class A:
            def bwahHaHa(self):
            """When called by callMe(), derived classes don't get a look in."""
            print "Bwah-Ha-Ha!"

            def callMe(self):
            """Calls non-overridable method bwahHaHa()."""
            A.bwahHaHa(self ) ########## This is the interesting bit ##########

            class B(A):
            def bwahHaHa(self):
            """Vain attempt to override what callMe() does."""
            print "Squeak"

            b = B()
            b.callMe() # Prints "Bwah-Ha-Ha!"
            b.bwahHaHa() # Prints "Squeak"

            --
            Richie Hindle
            richie@entrian. com


            Comment

            • Axel Straschil

              #7
              Re: How to inheritance overwrite non virtual?

              Hello!
              [color=blue]
              > What you want seems to be
              > class KObject:
              > def bestCaptionlong (self):
              > # class made explicit to guard against overriding
              > return KObject.bestCap tion(self)
              >[/color]

              Yes, that behaves the way I excpected, thank's to you and the other
              for helping me!
              [color=blue]
              > Do-nothing accessor methods may be acceptable style in C++ but are
              > superfluous in Python.[/color]

              I'm afraid I'm still not thinking in python, I'll work on it ;-)

              Lg, AXEL.

              Comment

              Working...