I asked for something similar recently, and staticmethod was the answer (because your baz and bar don't seem to be methods). Basically, you want to do something like a strategy template and call another function from run ().
def bar():
print "I am bar()"
return True
def baz():
print "I am baz()"
return True
class Foo(object):
torun = staticmethod(bar)
def run(self):
self.torun()
foo = Foo()
foo.run()
foo2 = Foo()
foo2.torun = baz
foo2.run()
output:
I am bar()
I am baz()
You can assign almost anything you want to your foo instance, including functions defined in another module. And the launch may take * args and ** kwds for more flexibility.
source
share