Home  >  Article  >  Backend Development  >  Detailed explanation of parameters in python functions

Detailed explanation of parameters in python functions

Y2J
Y2JOriginal
2017-05-08 16:33:231172browse

When I was reading "Python Core Programming" yesterday, I happened to see the function part, so I will summarize all the centralized parameter types that I have come into contact with so far^^

(1) Positional parameters, when calling the function, pass in the parameters by position

(2) Default parameters , that is, the value of the parameter is given when the function is defined. There are two points to pay attention to when setting the default parameters. First, the required parameters come first and the default parameters come 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 default parameters are provided out of order, the parameter names need to be written (that is, in the form of assignment). The default parameters must point to unchanged parameters (i.e. unchangedobject, the data inside the object cannot be changed once created, and no locking is required to read the object 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 for loop to access. If you use variable parameters directly, defining variable parameters only requires adding an * 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 with 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 separator *, 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})

【Related recommendations】

1. <a href="http://www.php.cn/course/list/30.html" target="_self"> Python free video tutorial</a>

2.<a href="http://www.php.cn/course/306.html" target="_self">Marco Education python basic grammar full explanation video</a>

##3. Python basic introductory tutorial<a href="http://www.php.cn/course/32.html" target="_self"></a>

The above is the detailed content of Detailed explanation of parameters in python functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn