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?
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