Arguments in python fast.ai function that are not in the function definition?

I have been coming across function calls that use arguments that are not in the function definition. I would like to know how that works (i.e. how the compiler interprets this).

For example, this function call:

 interp.plot_confusion_matrix(figsize=(12,12), dpi=60)

uses the variables "figsize" and "dpi", but neither of them turn up in the definition of plot_confusion_matrix: help(interp.plot_confusion_matrix) gives:

plot_confusion_matrix(normalize: bool = False, title: str = 'Confusion matrix', 
cmap: Any = 'Blues', norm_dec: int = 2, slice_size: int = None, **kwargs) - None 
method of fastai.vision.learner.ClassificationInterpretation instance
    Plot the confusion matrix, with `title` and using `cmap`.

Why is it not an error to use "figsize" and "dpi" in the function call if they are not arguments of the function? How does this work in python?

Topic fastai jupyter python

Category Data Science


Those parameters are taken care of by **kwargs in the function definition. You can look how this is dealt with by the code in the fastai repo and looking for **kwargs in the function definition.
Some explanations about how this works can be found on SO: https://stackoverflow.com/questions/3394835/use-of-args-and-kwargs.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.