Let's 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)
The output results are 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, 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 nice way 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.
"Life is short, I use Python." ”
Python args Kwargs The difference between passing parameters