Python---function---default parameters

Source: Internet
Author: User

#  default parameter #  since we often calculate x2, it is possible to set the default value of the second parameter N to 2def power (x, n=2):    s  = 1    while n > 0:         N = n - 1        s = s * x     return s#  So, when we call Power (5), it's equivalent to calling power (5, 2) print (' Print power (5) ' ,  power (5)) print (' Print power (5,3) ',  power (5, 3)) #  as can be seen from the example above, the default parameters have simplified the function's invocation. #  Set default parameters, there are a few points to note that the #  is a required parameter in front, the default parameters in the after, otherwise the Python interpreter will be error (default parameters placed in front will produce ambiguity ~ ~) #  Two is how to set the default parameters #  when the function has more than one parameter , the parameter with large change is put in front, and the parameters with small change are put back. A parameter with a small change can be used as the default parameter #  what are the benefits of using default parameters? The biggest benefit is the ability to reduce the difficulty of calling the function #  define the function enroll, set the age and city as the default parameter Def enroll (name, gender, age=6, city= ' Beijing '):     print (' Name: ',  name)     print (' Gender: ',  gender)     print (' Age: ',  age)     print (' CITY ',  city) #  So, most students do not need to provide age and city at the time of registration, only two parameters must be provided enroll (' Sarah ',  ' F ') #  only students who do not match the default parameters need to provide additional information #   When there are multiple default parameters, the call can either provide default parameters in order, or provide default parameters enroll (' Bob ',  ' M ',  7) #  when the partial default parameters are not provided sequentially. You need to write the parameter name on enroll (' Adam ',  ' M ',  city= ' Tianjin ') #  The following code shows a pit in the default parameter Def add_end (l=[]):     l.append (' END ')     return L#  first call Add_endprint (Add_end ()) #  The second call to Add_endprint (Add_end ()) # python function when defined, the value of the default parameter L is computed, that is [], because the default parameter L is also a variable, which points to the object []#  each call to the function, If the content of L is changed, the contents of the default parameter will change the next time it is called, no longer the function definition [] #  So, define the default parameters to keep in mind: the default parameter must point to the immutable object #  to modify the above example, we can use the none of the immutable object to achieve Def  add_end (L=none):    if l is none:         l = []    l.append (' END ')     return lprint ( Add_end ()) print (Add_end ())


Python---function---default parameters

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.