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 ".
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.
(go) parameters in Python: *args and **kwargs