2

An overridden method completely replaces the parent class' one:

class Msg:

    def proc(self):
        # this part is common to all methods
        print("common part")
        # I would like the override to start here
        pass

class A(Msg):

    def proc(self):
        # this should be appended below pass above
        print("overridden part")

A().proc()

Is it possible to partially replace the method, by keeping a "common" section and appending a specific one (per class)?

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 1
    Use `super`, access the superclass method using `super().methodname()` – cs95 Sep 09 '18 at 07:50
  • @coldspeed: thanks, you mean not overriding at all but just calling `super().methodname()` in the child class' method (which would have a different name than `methodname()`? I was hoping for the ability to append to the parent method (if this is at all possible) – WoJ Sep 09 '18 at 07:56
  • Yes. In your case, `super().proc()` should be the first line in the child's `proc` function. – cs95 Sep 09 '18 at 07:57
  • Here is the code to solve: [Code](https://repl.it/repls/MagentaWorriedPrinter) – U13-Forward Sep 09 '18 at 07:58
  • @coldspeed: ok thanks. I though I could do it top-down (= having the parent `proc` manage this inclusion, so that the child class is "standalone" (without this forced relationship to the parent)). But that's ok. – WoJ Sep 09 '18 at 08:01
  • @U9-Forward: in the case of your code, the "common" part would always be called, not only in the `proc` method. – WoJ Sep 09 '18 at 08:01
  • @WoJ Then i don't know – U13-Forward Sep 09 '18 at 08:03
  • @coldspeed: I have never used decorators but that may also be a usecase here (though I do not know yet if they are not overridden as well). I will have a look. – WoJ Sep 09 '18 at 08:06

0 Answers0