For efficiency purposes, I don't want to use functools.partial for this task (adds overhead when using the function). I want to redefine a function's copy defaults arguments, by name.
For the moment I have the following :
import types
# http://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python
def copy_func(f, name=None, defaults=None):
return types.FunctionType(f.__code__, f.__globals__, name or f.__name__, defaults or f.__defaults__, f.__closure__)
def f(a, b, c):
return 'a = {}, b = {} and c = {}'.format(a,b,c)
i = copy_func(f, 'i', (4,5))
print(f(3,4,5))
print(i('should be a'))
Can we modify the copy_func tool to be called like so :
i = copy_func(f, a = 4, b = 5)
i.e, using the names of arguments from the original function.
Of course, if the arguments do not exists, I hope the function would yell at me ;)