I have a foo
function that returns a value that is then used as input in a bar
function. In short, baz = bar(foo(•), •)
.
Now, as I want to test multiple foo
iterations with a bar
that stays the same, I created the make_baz
function in order to return baz
depending on the iteration. (i.e. baz_i = bar(foo_i(•), •)
, etc.)
The arguments and keywords are not always the same and I can not simply pass a dictionary as I am using scipy.optimize.curve_fit.
I have found a way to theoretically do what I want (see below), but it seems I can not define a function with exec
inside another function (it works as expected while in global)..
def foo_1(x, arg1, arg2, kwarg1=None):
y = "some function of x"
return y
def foo_2(x, arg1, kwarg1=None, kwarg2=None):
y = "some other function of x"
return y
def bar(y, bar_arg):
z = "function of y"
return z
def make_baz(foo):
args = list(inspect.signature(foo).parameters)
args_str = ", ".join(args)
kwargs_str = ", ".join([f"{arg}={arg}" for arg in args])
baz = f"def baz({args_str}, bar_arg):\n" \
f" y = {foo.__name__}({kwargs_str})\n" \
f" return bar(y=y, bar_arg=bar_arg)\n"
exec(baz)
return baz
print(make_baz(foo_1))
print(make_baz(foo_2))
Problem: it returns strings and not actual functions.
"""
def baz(x, arg1, arg2, kwarg1, bar_arg):
y = foo(x=x, arg1=arg1, arg2=arg2, kwarg1=kwarg1)
return bar(y=y, bar_arg=bar_arg)
"""
"""
def baz(x, arg1, kwarg1, kwarg2, bar_arg):
y = foo(x=x, arg1=arg1, kwarg1=kwarg1, kwarg2=kwarg2)
return bar(y=y, bar_arg=bar_arg)
"""
Question: do you have any workaround/solution ?