Python Advanced - Closures

Python Advanced - Closures Introduction:

We know that when the function is called, the variables defined in the function are destroyed, but we sometimes need to save the variable in the function, and complete some series of operations on the basis of this variable each time , such as: every time in this Based on the variable and other numbers, what should we do? We can solve this requirement through the closure we learned today.

1. Python Advanced - Closures.Introduction to Closures


We have learned about functions before. We know that when the function is called, the variables defined in the function are destroyed, but we sometimes need to save this variable in the function, and every time we complete some series of operations on the basis of this variable, For example: what should I do every time I calculate the sum with other numbers on the basis of this variable?
We can solve this need through the closure we learned today.

Definition of closure:
Under the premise of function nesting, the inner function uses the variables of the outer function, and the outer function returns the inner function. We call this inner function using the outer function variable a closure .

2.Python Advanced - Closures. The conditions of the closure


Through the definition of the closure, we can know the formation conditions of the closure:
1.Under the premise of function nesting (functions are defined inside functions)
2.The inner function uses the variables of the outer function (including the parameters of the outer function)
3.outer function returns inner function

3. Python Advanced - Closures..Example code for a simple closure


# define an external function
def func_out (num1):
# define an inner function
def func_inner (num2):
# The inner function uses the outer function's variable (num1)
result = num1 + num2
print("The result is: ", result)
# The outer function returns the inner function, and the inner function returned here is the closure
return func_inner
# create the closure instance
f = func_out ( 1 )
# execute the closure
f( 2)
f( 3)
Running result :
The result is: 3
The result is: 4
Description of the closure execution result:
From the above output, it can be seen that the closure saves the variable num1 in the external function, and each execution of the closure is performed on the basis of num1 = 1.

4. Python Advanced - Closure.The role of closures
•Closures can save variables in the outer function and will not be destroyed when the outer function is called.
be careful:
•Since the closure references the variables of the external function, the variables of the external function are not released in time, consuming memory.

5. Summary
1.A closure is formed when the returned inner function uses the variables of the outer function
2.Closures can save variables of external functions
3.Standard format for implementing closures:
# external function
def test1(a):
b = 10
# inner function
def test2():
# The inner function uses the variable or parameter of the outer function
print( a, b)
# Return the inner function, the inner function returned here is the closure instance
return test2

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00