I have a function-object f
, which takes 4 numeric inputs and outputs two numbers. Maybe
def f(a, b, c, d):
return a+b, c+d
or maybe
def f(a, b, c, d):
return a*c, d*c
To be clear, I don't actually know what f
is, I just have it as an object.
I would like to create a new function-object, h
, such that h(a,b,c,d)=x*c+y
where (x,y)=f(a,b,c,d)
. The trouble is, I have no direct access to c
, only to f
.
def make_h(f):
???
return h
assert( make_h(f)(a,b,c,d) == f(a,b,c,d)[0]*c+f(a,b,c,d)[1])
Is it possible to do this in python? I have tried searching and reading some documentation, but have not found an answer (yet?).
EDIT: There is a simple answer (given below) when the signature of f is fixed. Suppose I had to do this to different functions, some with inputs (a, b, c, d), some with inputs (l, m, c), and maybe some with inputs (c, r). Would it still be possible to do what I want?