09:python Basic Leak check

Source: Internet
Author: User

1.1 Closures

1. Closure concept

1. An inner function is defined in an outer function, the inner function uses a temporary variable of the outer function, and the return value of the outer function is a reference to the inner function, thus constituting a closure

2. In general, in our cognition, if a function ends, everything inside the function is freed, and the local variables disappear.

3. But closure is a special case, if the outer function at the end of the discovery of its own temporary variables in the future will be used in the internal function, the temporary variable is bound to the internal function, and then end themselves.

2, Closure features    

1. Must have an inline function

2. Inline functions must refer to variables in external functions

3. The return value of the external function must be an inline function

#An example of a closed packet functiondefouter (a): B= 10defInner ():#The temporary variable of the outer function is used in the inner function .        Print(A+B)#The return value of the outer function is a reference to the inner function    returnInnerif __name__=='__main__': Demo= Outer (5) Demo ()# the#Here we call the outer function passed in parameter 5#at this point the outer function two temporary variable A is 5 B is 10, and created an inner function, and then the internal function of the reference back to the demo#at the end of the outer function, it is found that the intrinsic function will use its own temporary variable, the two temporary variables will not be freed, bound to the intrinsic function#we call the intrinsic function to see if the intrinsic function can use the temporary variable of the external function.#The demo saves the return value of the outer function, which is the reference to the inner function, which is equivalent to executing the inner function
Closure Instances

3, inside the closure of the function to modify the external function local variables

1. In the basic Python syntax, a function can read global data at will, but there are two ways to modify global data:
1) Global Declaration globally variable
2) Global variables are mutable type data can be modified when
2, in the case of closures using the following two ways to modify
1) in Python3, you can declare a variable with the nonlocal keyword, which indicates that the variable is not a variable in the local variable space, and that it needs to be looked up in a variable space.
2) in Python2, there is no nonlocal this keyword, we can change the closure variable to variable type data for modification, such as the list.

#modifying an instance of a closure variabledefouter (a): B= 10#both A and B are closure variablesc = [A]#here corresponds to the method of modifying the closure variable 2    definner ():#method One: nonlocal keyword declaration (python3)nonlocal b b+=1#Method Two: Modify the closure variable to a variable data type such as List (Python2)C[0] + = 1Print(c[0])Print(b)returnInner#The return value of the outer function is a reference to the inner functionif __name__=='__main__': Demo= Outer (5) Demo ()#6
internal functions in closures Modify external function local variables1.2 A copy in Python

09:python Basic Leak check

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.