I have the following situation in Python:
def func_0(x):
# connects to a third party software
val_1, val_2 = output_extracted_from_a_third_party_software
return [val_1, val_2]
def func_1(x):
return func_0()[0]
def func_2(x):
return func_0()[1]
another_third_party_func(func_1, func_2) # this is a function that I cannot modify
Facts:
- It is not possible for me to modify func_0 because it just extracts the output of a third party software (a computationally expensive process).
- As a part of an algorithm, I have to pass func_1 and func_2 as 2 separate arguments into another third party library.
I'm looking for a more efficient way to define func_1 and func_2, so that I avoid calling func_0 twice.
Thank you in advance.
EDIT: Memorization doesn't work in this case because x has to be a numpy array.
func_1andfunc_2should take the values it needs as arguments, then you can do somethng liekval1, val2 = func_0(x)then simply cally1 = func_1(x, val1)andy2 = func_2(x, val2)func_0fromfunc_1andfunc_2but are rather treating it like an array, and you don't use the values ofxpassed tofunc_1andfunc_2. †he key to your question seems to be if you can callfunc_0just once, but it is unclear if you want to call it only once with the same value for thexparameter it takes or with two differentxvalues. If you want to call it with the samexvalue, then you can use some sort of caching, and @JoranBeasley's answer is a great way to do this.