Python uses the default function value to implement static function variables-Python tutorial

Source: Internet
Author: User
This article describes how to use the default value of a function to implement static variables of a function in Python, for more information about how to use the default function value in Python to implement static function variables, see the following example:

I. default value of Python functions

Using the default value of a Python function allows you to easily write code when calling a function. in many cases, you only need to use the default value. Therefore, the default value of a function is used in python, especially in the middle of a class. When using a class, you can easily create a class without passing a bunch of parameters.

You only need to add "= defalut_value" after the function parameter name, and the default value of the function is defined. Note that parameters with default values must be placed at the end of the function parameter list. parameters without default values cannot be placed after parameters with default values, if you define it like that, the interpreter will not know how to pass parameters.

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 retries < 0: raise IOError, 'refusenik user'    print complaint

When you call the above function, you can modify the number of retries and the output prompt language. if you are too lazy, you do not need to change anything.

II. python uses the default function value to implement the function's static variable function

Python does not support static variables, but we can use the default value of the function to implement the function of static variables.
When the default value of a function is a variable class, the class content can be changed while the class name remains unchanged. (The memory area opened up does not change, but the content can change ).
This is because the default value of a function in python is only executed once (like static variables, static variable initialization is also executed once .) This is what they have in common.

Let's take a look at the following program snippet:

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 result is:

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

For better understanding, why is the output of "print f (5)" [1, 2, 3, 5?

This is because when "print f (4, ['x'])", the default variable is not changed, because the initialization of the default variable is only executed once (called by default for the first time), the memory zone (which we can call as the default variable) opened during initialization is not changed, therefore, the final output result is "[1, 2, 3, 5]".

I believe the examples described in this article will help you with 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.