I am using a library that requires two functions as inputs for a method where these two functions are evaluated multiple times. For example
def the_func ( H, dH ):
    many_iterations = 10
    for i in xrange( many_iterations ):
       # internal calculations to yield a value of x
       x = np.random.randn(10) # say!
       foo = H(x)
       bar = dH(x)
       # do something with foo and bar e.g
       print foo,bar
However, calculating H and dH shares a lot of code, and evaluating each is expensive, so I have them calculated inside a single function that returns both. As an example, consider this function that returns two values, that correspond to H and dH above.
def my_func ( x ):
    # lots of calculations...
    return x.sum(), x/2.
Without changing the_func (which is coming from a library), I would like to still calculate only one run of my_func when the_func evaluates H and dH. At the moment, I'm solving the problem calling the_func as
the_func ( H=lambda x: my_func(x)[0], dH=lambda x: my_func(x)[1] )
This works fine, but for each iteration inside the_func, it needs to my_func the same function twice with exactly the same argument. I would like to evaluate this function only once per iteration, but without changing any of the_func.
 
     
    