In Python, I want to have a method that builds a new function from an old one.
Input: a function like f(**kwargs) together with a tuple of pairs that takes a dictionary of keywords
Output: a new function that has the pairs from the tuple as arguments and default values.
What I have so far:
def f(**kwargs):
return 'test',kwargs['a'], kwargs['b']
def magic(f,argtuple):
arglist=",".join([str(ke)+" = "+str(va) for ke,va in argtuple])
keylist=",".join([str(ke)+" = "+str(ke) for ke,va in argtuple])
exec("def g("+arglist+"): return f("+keylist+")")
return g
It does what I want:
In [25]: f(b=4,a=3)
Out[25]: ('test', 3, 4)
In [26]: g=magic(f,(['a',1],['b',2]))
In [27]: g()
Out[27]: ('test', 1, 2)
In [28]: g(a=3,b=5)
Out[28]: ('test', 3, 5)
In [29]: import inspect
In [30]: print inspect.getargspec(f)
ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None)
In [31]: print inspect.getargspec(g)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=(1, 2))
However I don't want to build the string and don't want to use the exec function. What are other ways of achieving this?
gabsolutely have to have that signature or would it be fine if it accepted*argsand**kwargs?