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
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
- Propagating parameters from one function to another
- Decorators
- External functions (like matplotlib, dataframe css styling, ...)
- Class inheritance requires it sometimes