In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to customize the function method and parameters with default values in Python" is not understood by most people, so the editor summarizes the following, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to customize function methods and parameters with default values in Python" article.
I. the method of self-defined function in Python
In Python programming, you can use defined functions, or you can customize functions to achieve some special functions.
1 the syntax of custom functions
In Python, the syntax for a custom function is as follows:
Def function name (argument): function body
Where def is the keyword; followed by the function name, the function is called through the function name; the function name is followed by a parenthesis, inside the parentheses is the parameter of the function, and the data can be passed to the function through the parameters; the function body is the function realized by the function. It should be noted that (1) there is a colon after the parentheses (2) the function body should be indented.
2 implementation of user-defined function
The code in figure 1 customizes a function called func:
Where the function name of the custom function is func,i is an argument to func. The function of the func () function is to print out a number from 0 to iMur1, and the value of I is passed from the outside to the inside of the function.
3 call of custom function
Call the custom function func () through the code in figure 2:
Where the parameter of the func () function is set to 3, and the output is a number of 0-2; modify the parameter value of func (), as shown in figure 3.
Figure 3 modifies the parameter values of the func () function:
At this point, the output is a number of 0-4.
2. Functions with default values for parameters in Python
The method of customizing a function in Python above mentions that the custom function contains parameters in parentheses after the function name.
As shown in figure 1:
In the figure above, a function named func () is customized, which contains a parameter I. Next, you define a variable i1 with a value of 0, which takes i1 as an argument to the func () function, and the output here is 1.
1 parameter is a function with a constant default value
The parameter I of func () shown in figure 1 has a constant default value.
As shown in figure 2:
A function whose 2 argument is a constant default value
You can see from figure 2 that the parameter I of the func () function has a constant initial value of 0; when the parameter is not specified for the function, the default value of the parameter for the function is 0, that is, the output is 0; when the parameter is specified for the function, the function outputs the value of the specified parameter.
Figure 3 shows:
As you can see from figure 3, the default value of parameter I for the func () function is variable i1, while the value of variable i1 is 1, so if you do not specify an argument for the func () function, the default value for that function is 1. When the default value of the variable changes, the default value of the parameter does not change.
As shown in figure 4:
Figure 4 the variable changes and the default value remains the same
At this point, the default value variable i1 corresponding to the parameter of the func () function changes to 2, while the default value of the parameter of the func () function is still 1.
The above is about the content of this article on "how to customize function methods and parameters with default values in Python". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.