Unpacking is to extract each element of a sequence or map separately, and a simple use of a sequence unpacking is to extract the first or previous elements separately from the following elements, for example:
First, seconde, *rest = sequence
If there are at least three elements in the sequence, then after executing the above code, first = = Sequence[0], second = = Sequence[0], rest = = sequence[2:].
function receives indeterminate parameter
When the parameters of a function are indeterminate, you can use *args and **kwargs,*args without a key value, **kwargs has a key value.
#!/usr/bin/python#-*-coding:utf-8-*-import sysreload (SYS) sys.setdefaultencoding (' utf-8 ') ' when the parameters of the function are indeterminate, you can use the *args and **kwargs,*args do not have a key value, **kwargs has a key value. " def fun_var_args_kwargs (Data1, *args, **kwargs): print ' data1: ', type (data1), data1 print ' *args : ', type (args), args print ' **kwargs: ', type (Kwargs), Kwargs Fun_var_args_kwargs (' This is Data1 ', 2, ' 3 ', 4.0, K1 = ' value1 ', k2= ' value2 ') print '-------------' def print_args (*args, **kwargs): print args.__class__.__ name__, args, kwargs.__class__.__name__, Kwargs Print_args () Print_args (1, 2, 3, a= ' a ')
Printing results:
Data1: This is
data1*args:
(2, ' 3 ', 4.0) **kwargs:
{' K2 ': ' value2 ', ' K1 ': ' Value1 '}-------------tuple () dict {}tuple (1, 2, 3) dict {' A ': ' A '}