Analysis on the use of * args and * * kwargs in python
According to the analysis of the use of * args and * * kwargs in python, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
A brief introduction
* args and * * kwargs are mainly used for function definition. When the number of parameters passed into the function we need to define is uncertain, we can use * args and * * kwargs instead of the uncertain number of parameters. In fact, it does not have to be written as * args and * kwargs. Only the * (asterisk) in front of the variable is necessary. We can write * var and * * vars. Writing as * args and * * kwargs is just a popular naming convention.
Second use
2.1 * args
When the number of parameters of a function is uncertain and there is no need to specify a parameter name, the format of * args is the regular parameter val1 [, val2,val3....]
Def func_arg (farg, * args):
Print "formal arg:", farg
For arg in args:
Print "another arg:", arg
Func_arg (1, "youzan", 'dba')
Output
In [10]: args (1, "youzan", 'dba')
Formal arg: 1
Another arg: youzan
Another arg: dba
2.2 * * kwargs
You can use * * kwargs when the parameters of the function are named and the number is uncertain. The parameter format of * * kwargs is key1=value1, [key2=value2,key3=value3,....], and the function pair * * kwargs is parsed in a dictionary-like way.
Def func_kwargs (farg, * * kwargs):
Print "formal arg:", farg
For key in kwargs:
Print "keyword arg:% s:% s"% (key, kwargs [key])
Output
In [15]: func_kwargs (1, id=1, name='youzan', city='hangzhou')
Formal arg: 1
Keyword arg: city: hangzhou
Keyword arg: id: 1
Keyword arg: name: youzan
* * another function of kwargs is to convert parameters to dictionaries
In [1]: def kw_dict (* * kwargs):
...: return kwargs
...:
In [2]: print kw_dict (axiom 1, 2, 2, 3)
{'averse: 1,' centering: 3, 'baked: 2}
2.3 examples of synthesis
In [3]: def foo (* args, * * kwargs):
...: print 'args =', args
...: print 'kwargs =', kwargs
...: print'-'
...:
In [4]: foo (1, 2, 3, 4)
Args = (1,2,3,4)
Kwargs = {}
-
In [5]: foo (axiom 1, 2, 2, 3)
Args = ()
Kwargs = {'averse: 1,' canals: 3, 'baked: 2}
-
In [6]: foo (1, 2, 3, 4, 1, 2, 3, 4, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 2, 4, 3)
Args = (1,2,3,4)
Kwargs = {'averse: 1,' canals: 3, 'baked: 2}
-
In [7]: foo ('averse, 1, None, axi1, baked 2, cased 3)
Args = ('a', 1, None)
Kwargs = {'averse: 1,' canals: 3, 'baked:' 2'}
-
2.4 order of use
The order in which standard parameters are used with * args and * * kwargs. When we want to use all three parameters in the function at the same time, the order is as follows:
Func (fargs, * args, * * kwargs)
After reading the above, have you mastered the usage analysis methods of * args and * * kwargs in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!