07-default parameter, 07-Default

Source: Internet
Author: User

07-default parameter, 07-Default
Default Parameter

When calling a function, if the default parameter value is not passed in, it is considered as the default value. parameters with the default value must be located at the end of the parameter list.

Example 1:

#-*-Coding: UTF-8-*-def test (a, B): # define a function, input a and B result = a + B print ("result = % d" % result) test () # Call the function and give the real parameter test) root @ ubuntu:/home/python/codes/python basics-05 # python2 test. py result = 33 result = 44 result = 55

In this program, we found a defect. Why do we say this? We can see that two parameters are passed in when we call the test function, but they are all the same after the second parameter, so we can think: Because the second parameter is the same, can we modify the code so that the second parameter has the default parameter? That is, when we only input one parameter, the second parameter is the default parameter; when we pass in two parameters, what we want is passed in? The answer is yes.

#-*-Coding: UTF-8-*-def test (a, B = 22 ): # In this case, B = 22 is the default parameter result = a + B print ("result = % d" % result) test (11) test (22) test (33) root @ ubuntu:/home/python/codes/python basics-05 # python3 test. py result = 33 result = 44 result = 55

Example 2:

In the previous section, we mentioned that the help () function can view the documentation of the function. Let's take a look at it now:

In [1]: help (print) Help on built-in function print in module builtins: print (...) print (value ,..., sep = '', end = '\ n', file = sys. stdout, flush = False) # end = '\ n' indicates why we print a line feed, because it is the default parameter Prints the values to a stream, or to sys. stdout by default. optional keyword arguments: file: a file-like object (stream); defaults to the current sys. stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

Example 3:

In [2]: def test (a, B = 22, c = 33): # defines a function. In this case, B and c are default parameters, that is, the default value...: print ()...: print (B )...: print (c )...: In [3]: test (11, c = 44) # Call the function and input the value of a to 11 and the value of c to 44112244.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.