0

I have a class ParentA, ChildB, and ChildC. The relationship is shown in the figure. How can I overwrite the run() function in the ParentA through ChildC? enter image description here

class ParentA:
    def __init__(self):
        pass

    def run(self):
        print("ParentA")

    def other(self):
        pass

class ChildB(ParentA):
    def __init__(self):
        super().__init__()

    def play(self):
        pass


class ChildC(ChildB):
    def __init__(self):
        super().__init__()

# The new run function is imported from other file
def run(a=1, b=2):
    print("new method")
    # Do a lot of new things...


child_b = ChildB()
child_c = ChildC()
print(child_b.run())
print(child_c.run())

I want to implement a function similar to:

replace(ChildC.run(), run())

The outputs are:

new method
new method
Jiangtao Liu
  • 41
  • 1
  • 4
  • The indenting is suspicious here. Is the last `def run():` supposed to be a method of `ChildC`? If you want `ChildC` to have it's own `run()` method, just give it the method. – Mark Jul 25 '22 at 02:24
  • Thanks. The last run() function is imported from another file, and I added a comment – Jiangtao Liu Jul 25 '22 at 02:40
  • The question has nothing to do with inheritance. If the goal is simply to take an existing function, and modify the class so that it uses that function as a method - then yes, you can do that, and it doesn't matter what the base classes are. However, the function must take into account a `self` parameter. Please see the linked duplicate, and see also https://stackoverflow.com/questions/2709821. – Karl Knechtel Jul 25 '22 at 02:46
  • Thanks. I know how to do it. `ParentA.run = run` – Jiangtao Liu Jul 25 '22 at 03:44

2 Answers2

0

If you want ChildC to have a run method that has a different implementation than Parent.run, override the method in ChildC like so:

class Parent:
    def run(self):
        print('Run from Parent')

class ChildA(Parent):
    pass

class ChildB(ChildA):
    def run(self):
        super().run()
        print('Run from ChildB')


c = ChildB()
c.run()

For the sake of the example, I call super().run() from ChildB.run but if you do not want Parent.run to execute, just omit that line.

kingkupps
  • 3,284
  • 2
  • 16
  • 28
  • It's not clear this is what they are asking. They have a `run` function that takes no arguments. It seem like they would like to make this *existing* function a method of `ChildC`. But it's not really clear. – Mark Jul 25 '22 at 02:27
  • @Mark that's a fair point and I agree. Updated the answer to put a disclaimer at the top and will update if I notice any response to your comment in the question. – kingkupps Jul 25 '22 at 02:29
  • Thanks, but it's not what I needed. Because my run() function has a lot of arguments. I also have some other ChildXXX inherited from ChildB. ChildB is used by another process, and I almost can't change it. – Jiangtao Liu Jul 25 '22 at 02:35
  • "Because my run() function has a lot of arguments." Why does this cause a problem? "I also have some other ChildXXX inherited from ChildB." Why does this cause a problem? Either they should have the same `run` method (in which case, add it to `ChildB` instead, so that it will be inherited), or else they shouldn't (in which case, it doesn't matter what changes you make to `ChildC`, the other `ChildXXX` will not be affected). – Karl Knechtel Jul 25 '22 at 02:48
0

do you mean redefine a method run() in child Class to overwrite the parent class's method?

class ParentA:
    def run(self):
        print("ParentA")

class ChildB(ParentA):
    def run(self):
        print("ClassB")

dddddddd
  • 16
  • 2