Today to see the source of the time to find a *args and **kwargs, a look can know that the args is God horse, is the array of all parameters, Kwargs do not know, Google, a person's blog is relatively simple and clear, the second understand the ~~kwargs is when you pass in key The =value is a stored dictionary.
Add: Kwargs does not affect the parameter position example: Def test (A,*args,**kwargs): print a #print B #print c print args print kwargs test ( 1,2,3,d= ' 4 ', e=5) output: 1 (2, 3) {' E ': 5, ' d ': ' 4 '} means 1 or the value of parameter A, args represents the remaining value, and Kwargs represents a pair of key values after args. Original:http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html First Look at an example: def foo (*args, * * Kwargs): print ' args = ', args print ' Kwargs = ', kwargs print '---------------------------------------' if __name__ = = ' __main__ ': foo (1,2,3,4) foo (a=1,b=2,c=3) foo (1,2,3,4, a=1,b=2,c=3) foo (' A ', 1, None, a=1, b= ' 2 ', c=3) output the result as follows: args = (1, 2, 3, 4) kwargs = {} --- ------------------------------------ args = () kwargs = {' A ': 1, ' C ': 3, ' B ': 2} ----------- ---------------------------- args = (1, 2, 3, 4) kwargs = {' A ': 1, ' C ': 3, ' B ': 2} ------------- -------------------------- args = (' A ', 1, None) kwargs = {' A ': 1, ' C ': 3, ' B ': ' 2 '} --------- ------------------------------ You can see that these two are mutable parameters in Python. *args represents any number of nameless arguments, which is a tuple;**kwargs representing the keyword argument, which is a dict. And when using both *args and **kwargs, the parameter column must be *args before **kwargs, such as foo (a=1, b= ' 2 ', c=3, a ', 1, None,), which would prompt a syntax error "SyntaxError: Non-keyword arg after keyword arg ". Oh, know *args and **kwargs is what it is. There is also a very beautiful usage, that is to create a dictionary: def kw_dict (**kwargs): return kwargs print kw_dict (a=1,b=2,c=3) = = {' A ': 1, ' B ': 2, ' C ': 3} In fact Python has the Dict class, you can use Dict (a=1,b=2,c=3) to create a dictionary.
Go: Python: What are *args and **kwargs