[Python function learning Article] default parameters

Source: Internet
Author: User

python function: Default parameter: Retries=4this form of Def ASK_OK (prompt, retries=4, complaint='Yes or no, please!'):         whileTrue:ok=raw_input (Prompt)ifOkinch('y','ye','Yes'):                returnTrueifOkinch('N','No','NOP','Nope'):                returnFalse Retries= Retries-1            ifRetries <0: Raise IOError ('refusenik User'When print complaint is called: only the necessary parameters are given: Ask_ok ('Do you really want to quit?') gives an optional parameter: ASK_OK ('OK to overwrite the file?',2) or give all the parameters: Ask_ok ('OK to overwrite the file?',2,'Come on, only yes or no!'characteristics of default parameters: Important Warning: The default value is only assigned once.    This makes it different when the default value is a Mutable object, such as a list, a dictionary, or an instance of most classes. For example, the following function accumulates (before) the arguments passed to it during subsequent calls: Def F (A, L=[]): L.append (a)returnL print F (1) Print F (2) Print F (3This will print: [1]    [1,2]    [1,2,3] If you do not want to share the default values in subsequent calls, you can write functions like this: Def f (A, L=None):ifL isnone:l=[] L.append (a)returnL

Learning website:

Http://docs.pythontab.com/python/python3.4/introduction.html#tut-numbers

[Python function learning Article] 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.