Definition

{python}**kwargs allows you to pass an unspecified amount of arguments to a function. The arguments do not need to be known beforehand.

Example

Use with decorators

Implementation

from functools import wraps
import time

# decorator function to measure time taken of fct.
def timeit(func):
    @wraps(func)
    def timeit_wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        result = func(*args, **kwargs)
        end_time = time.perf_counter()
        total_time = end_time - start_time
        # first item in the args, ie `args[0]` is `self`
        print(f'Function {func.__name__} took {total_time:.4f}s to run')
        return result
    return timeit_wrapper

The decorator needs to pass all arguments to the function it calls. It doesn't know, on which function it will get applied and needs to propagate the arguments as well of course. Therefore we use {python}**kwargs.

Function call

The two stars will unpack the dictionary, allowing to programmatically store parameters and unpack them.

I don't actually approve of this use, its just an example
parameter_dict = {
	"name": "Bob",
	"age": 20,
	"major": "Computer Science",
}

cv = create_cv(**parameter_dict)

When to use