Given a list with function names, add them as functions to a module so that they can be invoked from the module using the function name as an argument.
from package import module_name
Functions = [ 'Add', 'Subtract' ]
for function in Functions:
def api_endpoint(*args):
return module_name.api(function, *args)
setattr(module_name, function, api_endpoint)
I want to be able to call module_name.Add(arg1, arg2)
and invoke module_name.api('Add', arg1, arg2)
.
How can I pass on this sort of introspection and dynamically add a function to a module? I tried setting it up this way, but any invocation of module_name.Add always invokes the last item in Functions (e.g. Subtract), which is incorrect.
I tried making api_endpoint a Class which overrides __new__
, but that did not help, since if __new__
does not return an instance of the class being created, __init__
is never invoked and I can't get instance variables.