Summary turn on the parameters of the python

From: https: //blog.csdn.net/gaotihong/article/details/95687282

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/gaotihong/article/details/95687282
By convention, to a directory:

table of Contents

1. Location parameter

2. The default parameters

3. Variable parameters

4. keyword arguments

5. Name the keyword arguments

6. The combination of parameters

Recently we experienced function parameters, therefore summarize some.

1. Location parameter


2. The default parameters
1. The first parameter is mandatory, default parameters in the post, otherwise the Python interpreter will complain (think about why the default parameters can not be placed in front of mandatory parameters);

2. When the function has a plurality of parameters, the parameters placed in front of a large change, a small change in the parameters put back. Small changes in parameters can be used as the default parameters.

3. There are a plurality of default parameters, when invoked, it may be provided in order default parameters, such as calling enroll ( 'Bob', 'M ', 7), meaning that, in addition to the name, gender these two parameters, and finally a parameter used in the parameters age, city Since no parameters, still use the default value.
Order may not provide some default parameters. When not in order to provide some default parameters, parameter names need to write. Like calling enroll ( 'Adam', 'M ', city = 'Tianjin'), meaning that, city pass in parameters values, other default parameters continue to use the default values

 

3. The variable parameters
defined and variable parameters define a parameter list or tuple compared, only adding a number before the parameter *. Received inside the function parameter is a tuple numbers, therefore, the function code is completely intact. However, when you call the function, you can pass any number of parameters, including the parameter 0

 

4. Keyword parameters
keyword parameter allows you to pass containing 0 or any number of parameter name parameter, these parameters are automatically assembled as keywords inside a dict function.

Keyword arguments What is the use? It can extend the functionality of the function. For example, in the person function, we can ensure that the received name and age of these two parameters, however, if the caller is willing to provide additional parameters, we can receive. Imagine you're making a user registration function, in addition to user name and age are required, other options are available, use keyword parameters to define the function will be able to meet the registration requirements.

 

5. Name the key parameters
for the caller keyword parameters, function parameters can be passed to any keyword unrestricted. As to what passed in the end, you need to check in by kw internal function.

 

 

6. The combination of parameters
these five parameters can be combined, but the order was given: mandatory parameters, default parameters, variable parameter, named keyword arguments and keyword arguments

 

 

 

 
----------------
Disclaimer: This article is CSDN blogger, "I can not see it." Original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/gaotihong/article/details/95687282

Guess you like

Origin www.cnblogs.com/gisoracle/p/11989506.html