Should be easy, but somehow I don’t get it. I want to apply a given function. Background is copy a class and applying a given method on the newly created copy.
Major Edit. Sorry for that.
import copy
class A:
def foo(self,funcName):
print 'foo'
funcName()
def Bar(self):
print 'Bar'
def copyApply(self,funcName):
cpy = copy.copy()
# apply funcName to cpy??
a = A()
func = a.Bar()
a.foo(func) # output 'Bar'
b = a.copyApply(foo) # new copy with applied foo
Note that your
A.foodoes not take the name of a function, but the function itself.In python,
a.foo()is the same asA.foo(a), whereais of typeA. Therefore, yourcopyApplymethod takes the unbound bar method as its argument, whereasfootakes a bound method.