On the python closures

1. function references
look at the following example, a function reference is understood:

def test1():
    print("--- in test1 func----")

# 调用函数
test1()

# 引用函数
ret = test1

print(id(ret))
print(id(test1))

#通过引用调用函数
ret()

operation result:

--- in test1 func----
140212571149040
140212571149040
--- in test1 func----

2. What is the closure of
the closure is to be able to read other function of internal variables functions. In simple terms, the concept of closure package is that when we define a function within a function, this function uses internal temporary variables of the outer function and external function's return value is a reference to the inner function, which we call closure
to let we combine an example to understand this concept will be easier:

# 定义一个函数
def test(number):

    # 在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包
    def test_in(number_in):
        print("in test_in 函数, number_in is %d" % number_in)
        return number+number_in
    # 其实这里返回的就是闭包的结果
    return test_in


# 给test函数赋值,这个20就是给参数number
ret = test(20)

# 注意这里的100其实给参数number_in
print(ret(100))

#注 意这里的200其实给参数number_in
print(ret(200))

operation result:

in test_in 函数, number_in is 100
120

in test_in 函数, number_in is 200
220

3. Understand closures
Let's look at a real example of closure to a better understanding of closures:

def line_conf(a, b):
    def line(x):
        return a*x + b
    return line

line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5))
print(line2(5))

In this example, the function line and variables a, b constitute closures. Creating closures, we line_conf by the parameters a, b illustrate the values of these two variables, and so we determined the final form of a function (y = x + 1 and y = 4x + 5). We only need to change the parameters a, b, you can get a different expression of linear function. Thus, we can see, the closure also has a role to improve the reusability of code.
If there is no closure, we need to create a linear function of time every time at the same time explain a, b, x. Thus, we need more transmission parameters, but also reduces the portability of the code.
Note:
Due to the closures cited local variable external function, the local variable is not released in an external function, memory consumption

4. How to modify the function of external variables
in a closure function, we are free to use temporary variables of the outer function bindings, but when we modify it, you will find the error. what happened?
Among the basic syntax of python, a function can be read without global data, but when you want to modify global data in two ways:

  • global declare global variables
  • Global variables are variable data type when you can modify

How can that modify the function of external variables it:

  • In python3 in, to use the keyword nonlocal, this variable is not a variable to represent the local variable space, we need to look up a layer of variable space variable.
  • In python2, no nonlocal keyword, we can change the variable type closure variable data modifications, such as a list.

Look at the following code:

In python3 in:

def counter(start=0):
    def incr():
        nonlocal start
        start += 1
        return start
    return incr

c1 = counter(5)
print(c1())
print(c1())

c2 = counter(50)
print(c2())
print(c2())

print(c1())
print(c1())

print(c2())
print(c2())

operation result:

6
7
51
52
8
9
53
54

In python2 in:

def counter(start=0):
    count=[start]
    def incr():
        count[0] += 1
        return count[0]
    return incr

c1 = closeure.counter(5)
print(c1())
print(c1())
c2 = closeure.counter(100)
print(c2())
print(c2())

operation result:

6
7
101
102

5. Closures what use
the code above, we do not have closure can be achieved, why do you want to use closures.
In fact, it can be seen from the above examples and summary: closure can enhance the reusability of code, increased portability, a simplified procedure. We want to define a function to represent a line y = kx + b, with closures, we need to give a time k, b value, and then gives the x, produces a straight line. And not every definition of a function. It should be noted that, due to the closure references local variables of the outer function, local variables of the outer function can not be released in time, it will consume memory.

Guess you like

Origin www.cnblogs.com/lenfoo/p/11233860.html