Unpacking and packing in Python functions

DEF count_name (* args, ** kwargs):   # when the function defined in the above uses parameter * or **, called packaged 
    "" " 
    find any number of integers and 
    : param args: positional parameters 
    : param kwargs: Key Word parameter 
    : return: 
    "" " 
    result = 0
     for item in args: 
        result + = item 

    for item in kwargs.values ​​(): 
        result + = item
     return result 


# Simple pass value 
# one_res = count_name (10, 20, 30, num1 = 40, num2 = 50) 
# print (f "result is: {one_res}") 

one_tuple= (10, 20, 30 ) 
one_dict = { " name " : 40, " num2 " : 50 } 

# one_res = count_name (one_tuple, one_dict) # Report error 
# use an asterisk * or two * at our function call *, It is the process of unpacking 
# 
Yuanzu , the list uses an asterisk, the dictionary {} unpacking uses two asterisks ** one_res = count_name (* one_tuple, ** one_dict)   # sequence type unpacking is in the function At the calling place, pass in the function definition 
print (f " result is: {one_res} " )

 

******* Please respect the original, if you want to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! ******* 

Guess you like

Origin www.cnblogs.com/shouhu/p/12740804.html