I don't know if the following is "more pythonic" or more efficient, but at least it does the job in this particular case:
from inspect import signature
def my_fun(x, a, b, c, d, e, f, g):
return x, a, b, c, d, e, f, g
def fixing_function(orig_function, fixing_dict):
orig_params = signature(orig_function).parameters
remainingArgs = [key for key in orig_params.keys() if key not in fixing_dict]
def new_function(*pos_args):
zipped = dict(zip(remainingArgs, pos_args))
new_args = zipped | fixing_dict
return orig_function(**new_args)
return new_function
fixed_function = fixing_function(my_fun, {"a": 100, "e": 200})
print(fixed_function(1, 2, 3, 4, 5, 6))
#output: (1, 100, 2, 3, 4, 200, 5, 6)
However, it does not fit OP's purpose, as can be seen from how print(getfullargspec(fixed_function)) results in FullArgSpec(args=[], varargs='pos_args', varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}) (nevertheless, it may be useful to other people).
In case someone else wonders, here is my attempt using eval to satisfy getfullargspec as well:
from inspect import signature, getfullargspec
def my_fun(x, a, b, c, d, e, f, g):
return x, a, b, c, d, e, f, g
def fixing_function(orig_function, fixing_dict):
orig_params = signature(orig_function).parameters
remainingArgs = [key for key in orig_params.keys() if key not in fixing_dict]
argSubtitute = [str(fixing_dict[key]) if key in fixing_dict else key for key in orig_params.keys()]
partialF = f"lambda {','.join(remainingArgs)}:{orig_function.__name__}({','.join(argSubtitute)})"
return eval(partialF)
fixed_function = fixing_function(my_fun, {"a": 100, "e": 200})
print(fixed_function(1, 2, 3, 4, 5, 6))
#output: (1, 100, 2, 3, 4, 200, 5, 6)
print(getfullargspec(fixed_function))
#output: FullArgSpec(args=['x', 'b', 'c', 'd', 'e', 'g'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
There should be some ways it can be improved.