1

I have a function which returns multiple values, can I use that function somehow directly in another function's argument list? When I try it (naively) I get:

def one() :
    return 3, 2
def two(a, b):
    return a + b
two(one())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-86-27980b86a2c0> in <module>()
  3 def two(a, b):
  4     return a + b
----> 5 two(one())

TypeError: two() missing 1 required positional argument: 'b'

Of course I can do something like

def one() :
    return 3, 2
def two(a, b):
    return a + b
a, b = one()
two(a, b)
martineau
  • 119,623
  • 25
  • 170
  • 301
dr jerry
  • 9,768
  • 24
  • 79
  • 122
  • As an explanation of *why* it is only one parameter, `one()` returns a single tuple - `return` really only returns one object, although of course that can be a tuple of other objects. – cdarke Apr 27 '18 at 19:26
  • As a matter of fact it's a duplicate of a duplicate. Sometimes its a bit hard to formulate my questions the right way especially when learning a new Programming langage for me. Thanks for the answers. – dr jerry Apr 27 '18 at 19:30

1 Answers1

6

Sure.

two(*one())

* is argument unpacking

Useful reading: Understanding the asterisk(*) of Python

fferri
  • 18,285
  • 5
  • 46
  • 95