Let's take a look at an example:
Copy Code code as follows:
>>> 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)
The output results are as follows:
Copy Code code 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 '}
-----------------------
As you can see from the examples above, these two are variable parameters in Python. *args represents any number of unnamed parameters, it is a tuple;**kwargs to represent the keyword parameter, it is a dict. When *args and **kwargs are used at the same time, the *args parameter columns must be called before **kwargs, such as foo (a=1, b= ' 2 ', c=3, a ', 1, None,), which prompts for syntax errors "SyntaxError: Non-keyword arg after keyword arg ". As shown: