I have a class that's being imported in module_x
for instantiation, but first I want to override one of the class's methods to include a specific feature dynamically (inside some middleware that runs before module_x
is loaded.
I have a class that's being imported in module_x
for instantiation, but first I want to override one of the class's methods to include a specific feature dynamically (inside some middleware that runs before module_x
is loaded.
Neither AndiDog's nor Andrew's answer answer your question completely. But they have given the most important tools to be able to solve your problem (+1 to both). I will be using one of their suggestions in my answer:
You will need 3 files:
class C:def func(self):#do something
from myClass import *
def changeFunc():A = C()A.func = lambda : "I like pi"return Aif __name__ == "importer":A = changeFunc()
from importer import *
print A.func()
The output of module_x would print "I like pi"
Hope this helps