Bob K.
Bob K.

Reputation: 33

Pass tuple to function that takes *args and **kwargs

I have two functions:

def foo(*args, **kwargs):
    pass

def foo2():
    return list(), dict()

I want to be able to pass the list and dict from foo2 as args and kwargs in foo, however when I use it like

foo(foo2())

or

foo(*foo2())

tuple returned by foo2 gets assigned to *args. How should I do it properly?

Upvotes: 3

Views: 2664

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124758

You'll have to unpack your foo2() arguments into two names first, you can't directly use the two return values as *args and **kwargs with existing syntax.

Use:

args, kwargs = foo2()
foo(*args, **kwargs)

Upvotes: 3

Related Questions