Sidon
Sidon

Reputation: 1366

How to call a function with a named parameters through another function in python?

Let's say I have some functions (with different numbers of arguments) like this:

def add(n1=None, n2=None):
    return n1+n2

And I need to call them through the other, like this: call_another(function_name, arguments)

call_another(add, n1=1, n2=1)

I thought something with **kwargs:

def call_another(f, **kwargs):
   # How to call f() sending the arguments? 
   return f(kwargs)  # Obviously, don't work

I can't see a way to send the named arguments in kwargs to target function.

Upvotes: 2

Views: 60

Answers (1)

Dmitry
Dmitry

Reputation: 2096

You should use **kwargs instead of kwargs. Artificial example:

>>> def ff(**kwargs):
...     print(kwargs)
>>> def call_another(f, **kwargs):
...     f(**kwargs)
>>> call_another(ff, a=1, b=2)
{'a': 1, 'b': 2}

Upvotes: 2

Related Questions