Zinatic
Zinatic

Reputation: 79

Can a function feed two values into another function?

I ran into a weird problem while trying to learn Python. I'm writing a piece of code that takes data from a .txt-file, feeds it into a function that modifies it heavily, and then feeds it to another function. It worked until I tried to split the data into two parts. I broke my code down and got this:

def func1(v1, v2):
    return(v1, v2)

def func2(v1, v2):
    return(v1, v2)

foo, bar = func1(func2("foo", "bar"))

func2 returns two values (in a tuple, I think?), but func1 needs two separated values - a tuple won't cut it. Is there a way to spit the tuple directly, or do I need to do the following?

foo, bar = func2("foo", "bar")
foo2, bar2 = func1(foo, bar)

There must be a better way... Thanks in advance.

Upvotes: 1

Views: 112

Answers (1)

Jared
Jared

Reputation: 26397

Unpack the tuple,

foo, bar = func1(*func2("foo", "bar"))
                 ^

Some more information in the docs.

Upvotes: 9

Related Questions