HOWTO · Python
How to Pass Kwargs to Another Function in Python
Learn how Python keyword arguments work, how **kwargs collects them in a dictionary, and how to forward that dictionary to another function.
On this page
To pass a dictionary of keyword arguments to another Python function, call it with **mapping, as in display(**details). Python unpacks each key-value pair into a named argument. A function can collect a varying set of such arguments with a **kwargs parameter and forward them the same way.
Keyword Arguments in Python
Keyword arguments associate values with parameter names in a function call. Write each one as name=value; for example, greet(first="Anne", age=27) passes the values by name. The function must define parameters that accept those names, and the values are assigned to the matching parameters regardless of their position in the call.
This style makes calls easier to read when a function has several parameters. It also lets a caller specify only optional parameters while leaving parameters with defaults unchanged. The names in a keyword call must match the function’s parameter names unless the function accepts additional keywords through **kwargs.
Call a Function with **kwargs in Python
Add ** before a parameter name in a function definition to collect keyword arguments that do not have individual named parameters. By convention, this parameter is called kwargs, but it can have any valid variable name. Inside the function, Python makes the collected values available as a dictionary.
For instance, def describe(**kwargs): ... can receive a call such as describe(name="Rabbit", sound="squeak"). The resulting dictionary has two entries, with name mapped to "Rabbit" and sound mapped to "squeak". This lets a function accept named options without listing each option in its signature. The function can inspect the dictionary, select values, or pass it to another function.
*args serves a related purpose for positional arguments: it collects extra positional values into a tuple. Use *args for positional values and **kwargs for keyword values; **args is not the corresponding spelling for collecting positional arguments.
Pass **kwargs to Another Function in Python
A function can forward a dictionary by placing ** before the dictionary name in the call. In the example, details is collected by info, then expanded when info calls show_details. The called function receives the dictionary entries as named parameters through its own **kwargs parameter.
def show_details(**kwargs):
for key, value in kwargs.items():
print(key, value)
def info(**kwargs):
show_details(**kwargs)
details = {"name": "Shen", "age": 27, "degree": "SE"}
info(**details)
name Shen
age 27
degree SE
The call info(**details) expands the mapping into keyword arguments for info. That function collects them in its local kwargs dictionary, and show_details(**kwargs) expands the same entries for the next call. The loop prints each key and value. Keep the dictionary’s keys aligned with the names accepted by the receiving function; if it declares explicit parameters, each key must match one of them or be accepted by that function’s **kwargs parameter.
The forwarding function does not need to know each key in advance. This pattern is useful for wrappers that add behavior around another call while passing along caller-provided options. If a wrapper also has explicit parameters, define them before **kwargs in its signature, then forward only the collected keyword arguments as needed.
Conclusion
Use name=value to pass an ordinary keyword argument, **kwargs in a definition to collect extra keyword arguments into a dictionary, and **mapping in a call to unpack a dictionary for another function. For positional arguments, use *args. Matching the mapping’s keys to the receiving function’s accepted parameter names is the key to reliable forwarding.