Details of the five parameters of python function

  • 2020-05-26 09:35:04
  • OfStack

(1) position parameter. When the function is called, the parameter is passed in according to the position

(2) default parameter, that is, the value of the parameter is given when the function is defined. When setting the default parameter, two points should be noted: 1 is the required parameter before, and the default parameter after. 2 is to put small changes in the parameters can be used as the default parameters. A function with a default parameter can be called without passing in the default parameter, and can be called as an assignment if the value of the default parameter needs to be changed. If you do not provide the default parameters in order, you need to write the parameter names (that is, in the form of assignments). The default parameter must point to an immutable parameter (i.e., an immutable object, whose internal data 1 cannot be changed once it is created, and which does not need to be locked to read the object at the same time in a multitasking environment)

(3) variable parameters, that is, the number of parameters passed in is variable. Due to the uncertain number of parameters, we can pass in the parameter as 1 list or tuple, and we can use for loop to access it. If you use mutable parameters directly, define the mutable parameters as opposed to defining list or tuple parameters by simply adding a * to the parameter. Inside the culvert number, the parameter received is 1 tuple, so the function code is completely unchanged, but any parameter can be passed in when the function is called, including 0 parameters. If you already have an list or tuple to call a mutable argument there are two ways, 1 is to take each argument separately and pass it in. 2 is to put a * in front of it and pass in the element list or tuple as a mutable parameter.

(4) keyword parameters, variable parameters allow you to pass in 0 or any parameters, these parameters in the function call automatically assembled into 1 tuple. Keyword arguments allow you to pass in 0 or any number of arguments with the parameter name, which is automatically assembled inside the function as 1 dict. If the keyword parameter passed in is dict, you can prefix that parameter in the function with two ** signs.

(5) name the keyword parameter, which is used to limit the name of the keyword. Unlike the keyword parameter **kw, the named keyword parameter requires a special separator *, which is treated as a named keyword parameter. If the function definition already has a mutable parameter, the following named parameter does not need a special separator *. The named keyword parameter must be passed in the parameter name, which is different from the location parameter. If no parameter name is passed in, the call will report an error.

Note: parameters are defined in the order of required parameters, default parameters (1 must use immutable objects), mutable parameters, named keyword parameters, and keyword parameters def f(a,b,c=0,*,d,**kw). For any function, it can be called in a form similar to fun(*arg,**kw), regardless of how its parameters are defined.

*arg is a mutable parameter; arg receives tuple

**kw is a keyword parameter; kw receives dict

Variable parameters can be directly passed in: fun(1,2,3), In addition, list or tuple can be assembled first, and then transmitted via *arg: func(*(1,2,3));

Keyword parameters can either be directly passed in: fun(a=1,b=2), In addition, dict can be assembled first and then passed in through **kw: function(**{‘a':1,'b':2})


Related articles: