1

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.

4
  • 3
    memoization is the term you probably need to google Commented Oct 5, 2022 at 17:18
  • func_1 and func_2 should take the values it needs as arguments, then you can do somethng liek val1, val2 = func_0(x) then simply call y1 = func_1(x, val1) and y2 = func_2(x, val2) Commented Oct 5, 2022 at 17:22
  • @Moshmet it would probably good if you edited your question with more real code as people are getting hung up the fact that you are returning a list and that val1 and val2 are not defined Commented Oct 5, 2022 at 17:25
  • 1
    Your code is confusing: You're not calling func_0 from func_1 and func_2 but are rather treating it like an array, and you don't use the values of x passed to func_1 and func_2. †he key to your question seems to be if you can call func_0 just once, but it is unclear if you want to call it only once with the same value for the x parameter it takes or with two different x values. If you want to call it with the same x value, then you can use some sort of caching, and @JoranBeasley's answer is a great way to do this. Commented Oct 5, 2022 at 17:27

2 Answers 2

2

you can use memoization to solve this ... i think just the builtin LRU cache would work

@functools.lru_cache
def func_0(x):
    do something using x
    return [val_1, val_2]

of coarse this only works if x is the same in both calls ...

you can see it work as follows

import functools

@functools.lru_cache
def func_0(x):
    print("do something using x")
    return [x,x+1]

print(func_0(5)) # see print 'about do something using x'
print(func_0(6)) # see print 'about do something using x'
print(func_0(5)) # DO NOT see print 'about do something using x'
Sign up to request clarification or add additional context in comments.

2 Comments

I re-formulated the question, so that people don't get confused. Do you think it is clearer now?
Unfortunaltey it doesn't work because x is a numpy array (unhashable type).
-1

Could you be looking for Tuple unpacking?

2 Comments

i believe you are trying to help him solve the wrong problem
This isn't an answer to the question. This should be in a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.