Python method for implementing a function static variable using the function default value

Source: Internet
Author: User
This example shows how Python uses function defaults to implement static variables of a function, as follows:

First, the Python function default value

The use of the Python function defaults can be handy for writing code when the function is called, and many times we just use the default values. So the default value of the function is much used in Python, especially in the middle of the class, where a bunch of the initialization functions of the class will use the default values. You can easily create classes with classes without passing a bunch of arguments.

As long as you add "=defalut_value" after the function parameter name, the function default value is defined. One place to note is that parameters with default values must be at the end of the list of function arguments and not allow parameters that have no default values to be placed on the parameter with default values, because if you define that, the interpreter will not know how to pass the arguments.

Let's take a look at the sample code:

def ask_ok (prompt, retries=4, complaint= ' Yes or No, please! '): While  True:    OK = raw_input (prompt)    if OK in (' Y ', ' ye ', ' yes '): return True    if OK in (' n ', ' No ', ' nop ', ' Nope '): return False    retries = retries-1    if Retrie s < 0:raise IOError, ' refusenik user '    print complaint

When you call the above function, you can modify the number of retries and the output of the prompt language, if you are more lazy words, then nothing to change.

Second, Python uses the function default value to implement function static variable function

Static variables are not supported in Python, but we can implement the function of static variables through the default values of the functions.
When the default value of a function is a class whose contents are mutable, the contents of the class are mutable, and the name of the class does not change. (equivalent to the open area of memory that does not change, and where content can vary).
This is because the default value of a function in Python is only executed once, (as with static variables, static variable initialization is performed once.) This is what they have in common.

Then look at the following program fragment:

Def f (A, l=[]):  l.append (a)  return L print f (1) print F (2) print F (3) print F (4,[' X ') print F (5)

The output is:

[1] [1, 2] [1, 2, 3] [' X ', 4] [1, 2, 3, 5]

The previous good understanding of why the final "Print F (5)" Output is "[1, 2, 3, 5]"?

This is because "Print f (4,[' X"]) ", the default variable is not changed, because the default variable initialization is only performed once (the first time using the default value call), the initialization of the execution of the Open memory area (we can call the default variable) has not been changed, so the final output is" [1, 2 , 3, 5] ".

It is believed that the examples mentioned in this paper will help the Python program design.

  • 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.