I would like to create functions in my Python3 code, making use of data supplied during run-time. I'm stuck on how I can write a function along the lines of
def foo(varname1, varname2):
return varname1 + varname2
where the strings varname1 and varname2 that give the parameter names are specified as arguments to some constructor function, e.g.:
def makeNewFooFunc(varname1, varname2):
# do magic
return foo
fooFunc = makeNewFooFunc('first', 'second')
print(fooFunc(first=1, second=2))
# should return 3
what would be the #do magic step? Is this a job for eval, or is there an alternative?
**kwargs, without needing to "generate a function" with that specific signature.your_function(**params), which meansdef your_function(**kwargs)should work just fine. Also, why can't you simply write those function signatures accordingly by hand? Presumably each of these functions is unique anyway, so why the need to generate them?