Search code examples
python-3.xfunctiontuplesreturnsimulation

How to pass a tuple of multiple returned values from a function_1 to function_2, in a single call of function_1 in function_2


I have a function_1 as this:

def my_func_1():
    a = 2
    b = 3

    r1 = a+b
    r2 = a*b

    return r1, r2

I want to call this function_1 in another function_2. The issue is I can call the function_1 returned values as this in function_2:

import my_func_1 *

def my_func_2():
    result_1 = my_func_1()[0]
    result_2 = my_func_1()[1]

The issue is my_func_1() is reasonably computational heavy. I should be able to call the function_1 once while assigning the different returned values to different variables in function_2. How may I do this. Thanks in advance for the help.


Solution

  • Ahhh, this is so easy, I got it:

    import my_func_1 *
    
    def my_func_2():
        result = my_func_1()
        result_1 = result[0]
        result_2 = result[1]