When the parameters of a Python function are indeterminate, you can use *args and **kwargs to refer to an indefinite number of arguments.
The difference between the two is that *args is a tuple (tuple), and **kwargs is a dict (dictionary). To put it simply, it is *args without Key,**kwargs with key.
First verify that the above conclusions are correct by code:
def Func_args (*args): print(type (args))def Func_kwargs (* * Kwargs): print(type (Kwargs))if__name__'__main __': Func_args (1, 2, 3) Func_kwargs (a=1, b=2, c=3)
Running results, you can clearly see that args is a tuple, Kwargs is a dictionary
<class'tuple'><class'dict' >
Then try to use the *args, implement a function, and return the value of the passed parameter X2. Because the number of arguments passed in is variable, the result returned by the function is also variable.
def func_a (*args): return Tuple (ARG * 2 for arg in Span style= "color: #000000;" > args) if __name__ = = '
In Func_a, a tuple is returned, and the values of the tuple are assigned to variables A and B, respectively, using the function of the Python tuple to automatically unpack the packets.
Operation Result:
2 4 AA bb
Python's *args and **kwargs