Python uses the function default value to implement the function static variable method, python Default Value

Source: Internet
Author: User

Python uses the function default value to implement the function static variable method, python Default Value

This example shows how Python uses the default function value to implement static function variables. The specific method is as follows:

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.


Python defines A function A. function B has A variable. How can I obtain the value of A variable in function A during function calling? What is the value of A database?

Your question is a bit confusing. The recommended answer should be a positive solution. However, some friends will reply to you using classes to express them later. I think they are all right. First, you need to understand the Organizational Relationship of your data structure.

What needs to be hidden and why. Do not hide any consequences. Then you want to use the function + variable method or the class method.

It is clearly stated that the function does not have static variables. Therefore, there is no variable in the function. Several other functions can be referenced. This variable is closed and hidden. This is a bit confusing.

As long as you think clearly, you will find the answer by yourself.
Def fun1 (parameter1 ):
Var B
B = 333
Return B

Def fun2 (parameter2 ):
C = fun1 (parameter2 * 3)
Return c
I wonder if you want to express this result.

How does one function use the variables in another function in python?

For example, if a function has a variable I, how can it be called or changed outside the function or in another function. Thank you for learning.

====================================

You cannot change the local variables in a function outside the function, because when you instantiate the function, the local variables in the function will be assigned a new value and redefined. How can you change the local variables?

If you want to implement all the variables and local variables, you must use global variables (self in the class ):

==================== Bianliang. py ================
# Coding: UTF-8
I = 0 # define a global variable I

Def ():
'''
Print the I value
'''
Print I

Def B ():
Global I

A () # Call method
Print I # print I
I = 1000 # re-assigning I
Print I # print I
A () # Call method a again

B ()
======================================

% Python bianliang. py
0
0
1000
1000

This py file uses a global variable I to implement the shared parameter I of method a and method B.

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.