python *and**

1. *args and **kwargs

  The special meanings of *args and **kwargs in python are related to functions, but have different meanings in function calls and function declarations.

  Generally speaking, whether it is a function call or declaration, a single asterisk indicates the presence of a tuple (or list), while two asterisks indicate the presence of a dictionary.

Second, the function call:

* and ** in function calls

E.g:

def check_web_server(host, port, path) :  
Use check_web_server('127.0.0.1', 8000, '/admin/') to call this function.

 

 Some information is in triples, such as:

host_info= ('www.python.org', 80, '/')             #http://www.python.org/  
The call will then become:  
check_web_server(host_info[0], host_info[1], host_info[2])  

 

1. Single asterisk

  This way of writing is not scalable, at this time we can use a single asterisk to solve this problem. Because when the function is called, the expression turns it on when evaluating an asterisk-prefixed tuple or list.

So the above tuple function call, we can write its equivalent form:

check_web_server(*host_info)

 

2. Double asterisk

  The usage of the dictionary corresponding to the double asterisk is similar. Now we create a dictionary similar to ('www.python.org', 80, '/')

host_info= {'host': 'www.python.org', 'port': 80, 'path': '/'}  

 

 So the function call becomes:

check_web_server(**host_info)

 

 It tells the function that when opening the dictionary, each key is the name of the parameter, and the corresponding value is the parameter of the function call. It is equivalent to:

check_web_server(host='www.python.org', port=80, path='/')

 

 三、函数的声明

  函数声明里的*和**

  函数声明里的*和**虽然相似但作用不同:它们让Python得以支持变长参数(varargs),即函数可以接受任何数量的参数。

  当定义一个有三个参数的函数时(没有默认值的参数),调用的时候必须传入正好三个参数。默认参数虽然引入了一些灵活性,但函数依然受制于所定义参数的最大数目。

  如果需要更大的灵活性,我们可以用单星号表示的元组来定义一个变长参数,这个元组包含了所有的元素。现在我们来创建一个这样的“daily sales total”函数

def daily_sales_total(*all_sales) :   
    total = 0.0  
    for each_sale in all_sales :  
        total += float(each_sale)  
    return total  

 

 相应的合法的函数调用有:

daily_sales_total()  
daily_sales_total(10.00)  
daily_sales_total(5.00, 1.50, '128.75')  #Any type is allowed,not just flosts  

 

 不管你向这个函数传递多少参数,他都能够处理。all_sales就是一个包含了所有参数的元组(这就是我们要在函数定义里迭代all_sales的原因)。

  你还可以把普通参数和和变长参数混在一起使用,这时候vararg就会捕捉所有剩下的参数,例如现在这个假设的check_web_server函数定义就能接受额外的参数了。

def check_web_server(host, port, path, *args):  

 

  Note: When using variable-length parameters in a function definition, all required parameters must appear first, then the parameters with default values, and finally the variable-length parameters.

  Similarly, you can use double asterisks in a function declaration to accept any number of keyword arguments, which will be imported into another dictionary when the function is called.

def check_web_server(host, port, path, *args, **kwargs):

 

 The function must accept the initial three arguments, but can also accept any number of subsequent arguments or keyword arguments: inside the function, we can examine the contents of the args tuple and the kwargs dictionary respectively to decide whether to discard them.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690077&siteId=291194637