keven ren keven ren - 7 months ago 8
Python Question

Input of a function contains output of another function

A bit of a general question that I cannot find the solution for,

I currently have two functions

def func1(*args, **kwargs):
...
return a,b


and

def func2(x,y):
...
return variables


I would like my code to evaluate

variables = func2(func1())


Which python does not accept as it says func2 requires two arguments when only one is given. My current solution is doing an intermediate dummy redefinition but makes my code extremely cluttered (my "func1" has an output of many parameters).

Is there an elegant solution to this?

Answer
def func1():
    return 10, 20


def func2(x, y):
    return x + y

results = func2(*func1())
print results

--output:--
30

A function can only return one thing, so func1() actually returns the tuple (10, 20). In order to get two things, you need to explode the tuple with *.

Comments