*args and **kwargs in python

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.

 If this 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='/')

 Third, the declaration of the function

  * and ** in function declaration

  * and ** in function declarations are similar but have different functions: they allow Python to support varargs, meaning that functions can take any number of arguments.

  When defining a function with three parameters (parameters without default values), it must be called with exactly three parameters. Although default parameters introduce some flexibility, the function is still limited by the maximum number of parameters defined.

  If more flexibility is required, we can define a variable-length parameter with a tuple denoted by a single asterisk, which contains all the elements. Now let's create a "daily sales total" function like this:

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

 The corresponding legal function calls are:

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

 No matter how many parameters you pass to this function, it can handle it. all_sales is a tuple containing all the parameters (this is why we are iterating over all_sales in the function definition).

  You can also mix normal arguments with varargs, in which case vararg will capture all remaining arguments, e.g. this hypothetical check_web_server function definition now accepts extra arguments.

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=326756269&siteId=291194637
Recommended