args, kwargs variable parameters list of tuples, dictionaries transmission

Original link: http://www.cnblogs.com/pinpin/p/10287555.html

* Args, ** kwargs uncertain parameters are expression methods, generally used for the function parameter. * Args, ** kwargs passed as function arguments, during use, can be on the * args, ** kwargs a plurality of inputs, but the efficiency or trouble, it is possible to use the list, tuple, dict type variable is passed to args, kwargs, then the * args, ** kwargs directly passed as an argument to the function.

* Args tuple or list can be used to accommodate a plurality of variable composition

** kwargs dict may be used to receive a plurality of key and value

Specifically how that directly * args, ** kwargs through the list, tuple or dict passed, look at an example:

# Variable and function definitions:

dict_t = {'one':1,'two':2,'three':3}
tuple_t = ('one','two','three')
list_t = ['one','two','three']

def list_tuple(*args):
    print(args)

def tdict(**kwagrs):
    print(kwagrs)

#Instructions:

list_tuple (* tuple_t) #tuple type variable is passed to args
list_tuple (* list_t) #list type variable is passed to args
tdict (** dict_t) # dict type variable to kwargs

Results of the:

('one', 'two', 'three')
('one', 'two', 'three')
{'one': 1, 'two': 2, 'three': 3}

Eval manner may also be used, the specific description eval built-in function can be viewed in the introduction: https://www.cnblogs.com/pinpin/p/10287534.html

dict_t = {'one':1,'two':2,'three':3}
tuple_t = ('one','two','three')
list_t = ['one','two','three']

def list_tuple(*args):
    args = eval('args')
    print(args)

list_tuple (list_t)
list_tuple (list_t)
list_tuple(dict_t)

Results of the:

(['one', 'two', 'three'],)
(['one', 'two', 'three'],)
({'one': 1, 'two': 2, 'three': 3},)
 
Eval difference is the use of: * args, ** kwargs transmitting as a string, of course, to read data using a multi-step:
list_t = ['one','two','three']
def list_tuple(*args):
    args = eval('args')
return args

tets = list_tuple (list_t)
for i in range (len (tets [0])): #tets [0] of the length of the first element, the reason, the eval transmission data is transmitted as a character
print (Tet [0] [i])
 
Results of the:
one
two
three
 

Reproduced in: https: //www.cnblogs.com/pinpin/p/10287555.html

Guess you like

Origin blog.csdn.net/weixin_30892889/article/details/94790143