2
def function_1(a,b,c,d):
    print('{}{}{}{}'.format(a,b,c,d))
    return

def function_2():
   t=y=u=i= 5
   return t,y,u,i

function_1(function_2())

I expect that python would execute function 2 first, and return each t, y, u and i as inputs to function1, but instead I get:

TypeError: function_1() missing 3 required positional arguments: 'b', 'c', and 'd'

I understand that either the output of function2 is in a single object, or it is treating function2 as an input function, instead of executing.

how do I change my code to execute as expected? (each of the output variables from function2 treated as input variables to function1)

dy5es41
  • 87
  • 1
  • 7
  • 1
    Also *THANK YOU* for producing a minimal complete verifiable example. That's a *huge* thing that adds to ease of answering. – Adam Smith Nov 12 '18 at 01:06

2 Answers2

4

You need a splat operator.

function_1(*function_2())

function_2() returns a tuple (5, 5, 5, 5). To pass that as a set of four parameters (rather than one four-element tuple as one parameter), you use the splat operator *

Closely related is this question

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

This might be a slightly awkward way of doing it, but you can actually pss the function as a parameter itself, and then decompose it!

def function_1(func):
    print('{}{}{}{}'.format(*func))
    return

def function_2():
   t=y=u=i= 5
   return t,y,u,i

function_1(function_2())

output:

5555
LeKhan9
  • 1,300
  • 1
  • 5
  • 15