【Python】**kwargs和takes 1 positional argument but 2 were given

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:

1 class C():
2     def __init__(self, **kwargs):
3         print(kwargs)

 

We have the following inputs:

In [48]: c = C()
{}

In [49]: c = C(a = 1)
{'a': 1}

 

It's all common sense. But when I use a dictionary delivery time:

In [50]: c = C({'a': 1})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-2be6d5be2a84> in <module>()
----> 1 c = C({'a': 1})

 

I thought it was __init__ too squeamish, replaced by general approach:

1 In [51]: class C():
2     ...:     def f(self, **kwargs):
3     ...:         print(kwargs)
4     ...:

 

In [52]: c = C()

In [53]: c.f({'a': 1})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-5daee03baab1> in <module>()
----> 1 c.f({'a': 1})

TypeError: f() takes 1 positional argument but 2 were given

 

Including function is so defined under the __main__:

In [54]: def f1(**kwargs):
    ...:     print(kwargs)
    ...:

In [56]: f1({'a': 1})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-8652a6e75162> in <module>()
----> 1 f1({'a': 1})

TypeError: f1() takes 0 positional arguments but 1 was given

 

But when I added a parameter f1 in front of the parameter name :

1 In [57]: f1(b = {'a': 1})
2 {'b': {'a': 1}}

 

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:

1 In [58]: class C():
2     ...:     def __init__(self, kwargs):
3     ...:         print(kwargs)
4     ...:
5 
6 In [59]: c = C({'a': 1})
7 {'a': 1}

 

This experience deepened my understanding of the ** kwargs special parameters. After learning to pay attention to details!

Guess you like

Origin www.cnblogs.com/littleye233/p/11618990.html