Home > Net >  How to override a method in python of an object and call super?
How to override a method in python of an object and call super?

Time:12-01

I have an Object of the following class which inherates from the algorithm class.

class AP(Algorithm):
       def evaluate(self, u):
             return self.stuff *2  u

The Algorithm class has a method called StoppingCritiria.

At some point in the project the object objAP = AP() gets created. Later on I can then actually access it. And at that point in time I want to override the method StoppingCriteria by some function which calls the old StoppingCriteria.

I tried simply

def new_stopping(self):            
    return super().StoppingCriteria() and custom(self.u)

objAP.StoppingCriteria = newStoppingCriteria 

But that did not work. What did work were two rather inconviniend solutions:

  1. New AP class (not desirable since I possibly need to do that for lots of classes)

    class AP_custom(AP):
    def StoppingCriteria(self):
    return super().StoppingCriteria() and custom(self)

  2. Override the Method but not using super but rather copy pasting the code into the new function and adding my code to that. Not desirable since I want to changes in the original method to be applyed to my new function as well.

CodePudding user response:

See Override a method at instance level for many possible solutions. None of them will really work with super though, since you're simply not defining the replacement function in a class. You can define it slightly differently though for it to work:

class Foo:
    def bar(self):
        print('bar')

f = Foo()

def _bar(self):
    type(self).bar(self)  # or Foo.bar(self)
    print('baz')

from typing import MethodType

f.bar = MethodType(_bar, f)
f.bar()  # outputs bar baz

Since you're replacing the method at the instance level, you don't really need to access the method of the super class, you just want to access the method of the class, which still exists in its original form.

  • Related