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)

1
  • 1
    Also THANK YOU for producing a minimal complete verifiable example. That's a huge thing that adds to ease of answering. Commented Nov 12, 2018 at 1:06

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.