Python defined functions can be added to the parameters in simple terms ** kwargs-- purpose is to allow the added variable parameter parameter name , and as a dictionary to pass parameters. But only if - you have to provide the parameter name .
For example the following situation:
We have the following inputs:
It's all common sense. But when I use a dictionary delivery time:
I thought it was __init__ too squeamish, replaced by general approach:
Including function is so defined under the __main__:
But when I added a parameter f1 in front of the parameter name :
Solve the problem immediately.
Thinking:
In our previous mistakes attempts, it has been passed in a simple dictionary. Although ** kwargs provide the parameters turn into a dictionary function, but directly into the dictionary and it will not be understood as a parameter. Essentially Python interpreter think our input is used as the parameter value of the dictionary, but did not carry the parameter name , and Python function mechanism "Fixed name" parameter should be written on the front ** kwargs, so we pass the mistaken a "fixed name" parameter, in fact, our previous definition did not join the "fixed name" parameter, so the error Takes 1 But 2 Positional arguments the WAS gIVEN .
If you really want to pass the dictionary how to do it? This simple, directly ** kwargs into kwargs to:
This experience deepened my understanding of the ** kwargs special parameters. After learning to pay attention to details!