(1) Positional parameters, parameters are passed in according to position when calling the function
(2) Default parameters, that is, when the function is defined Give the value of the parameter and pay attention to two points when setting the default parameter. One is that the required parameter comes first and the default parameter comes last. The second is to put the parameters with small changes at the end and use them as default parameters. When a function with default parameters is called, the default parameters do not need to be passed in. If the value of the default parameter needs to be changed, the function can be called in the form of assignment. If the default parameters are not provided in order, the parameter names need to be written (that is, in the form of assignment). The default parameters must point to immutable parameters (i.e. immutable objects, the data inside the objects cannot be changed once created, and no locking is required when reading objects simultaneously in a multi-tasking environment)
(3) Variable parameters, that is, the number of parameters passed in is variable. Since the number of parameters is uncertain, we can pass the parameters in as a list or tuple, and use a for loop to access them. If you use variable parameters directly, defining variable parameters only requires adding a * sign in front of the parameters compared to defining list or tuple parameters. The internal parameter of the function receives a tuple, so the function code remains completely unchanged. However, any number of parameters, including 0 parameters, can be passed in when calling the function. If there is already a list or tuple and you want to call a variable parameter, there are two methods. One is to take out each parameter separately and pass it in. The second is to add a * sign in front of it to turn the elements of the list or tuple into variable parameters and pass them in.
(4) Keyword parameters, variable parameters allow you to pass in 0 or any number of parameters, and these parameters are automatically assembled into a tuple when the function is called. Keyword parameters allow you to pass in 0 or any number of parameters containing parameter names. These keyword parameters are automatically assembled into a dict inside the function. If the keyword parameter passed in is dict, you can add two ** signs in front of the parameter in the function.
(5) Named keyword parameters are used to limit the names of keywords. Unlike keyword parameters **kw, named keyword parameters require a special delimiter *, and the parameters following * are regarded as named keyword parameters. If there is already a variable parameter in the function definition, the named parameter that follows does not need a special separator *. Named keyword parameters must be passed in the parameter name, which is different from positional parameters. If no parameter name is passed in, the call will report an error.
Note: The order of definition of parameters is: required parameters, default parameters (immutable objects must be used), variable parameters, named keyword parameters, keyword parameters def f(a,b,c= 0,*,d,**kw), any function can be called in the form similar to fun(*arg,**kw), regardless of how its parameters are defined.
*arg is a variable parameter, arg receives a tuple
**kw is a keyword parameter, kw receives a dict
Variable parameters can be passed directly Input: fun(1,2,3),
You can assemble the list or tuple first, and then pass in *arg: func(*(1,2,3));
Keyword parameters can be passed in directly: fun (a=1, b=2),
You can also assemble the dict first, and then pass it in through **kw: function (**{'a':1,'b':2})
For more articles related to the 5 parameters of python functions, please pay attention to the PHP Chinese website!