given a module with a class Foo with a method that calls a function bar
defined in the module scope, is there a way to substitute bar
for a different function without modification to the module?
class Foo(object):
def run(self):
bar()
def bar():
return True
I then have a an instance of Foo
for which I would like to substitute a function baz()
for bar()
without having to modify the Foo
class.
Let's assume your module is called deadbeef
, and you're using it like this
import deadbeef
…
foo_instance = deadbeef.Foo()
Then you could do
import deadbeef
deadbeef.bar = baz
…