8 闭包

1. 函数引用   函数体

    

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

#调用函数
test1()

#引用函数
ret = test1

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

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


####运行结果:

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

2.闭包

  1)版本1:返回函数体

def test1(number):

    def test2():
        print(number+50)

    return test2

result = test1(100)
print(result)
## 运行结果
<function test1.<locals>.test2 at 0x7fbb510e86a8>   #函数体

  2)版本2:执行test_in()

def test1(number):
    print("--1--")
    def test_in():
        print("---2")
        print(number+50)

    print('---3')
    return test_in

result = test1(100)

print('-'*30)
result()
### 运行结果
--1--
---3
------------------------------
---2
150

  3)版本3:test_in(num2)

def test1(number):
    print("--1--")
    def test_in(num2):
        print("---2")
        print(number+num2)

    print('---3')
    return test_in   #返回函数2的引用

result = test1(100)   

print('-'*30)
result(30)
result(1)
result(33)
### 运行结果
--1--
---3
------------------------------
---2
130
---2
101
---2
133

    

   4)版本4:什么是闭包?

  内部函数对外部函数作用域里变量的引用(非全局变量),则称内部函数为闭包。

#定义一个函数
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))
运行结果:

in test_in 函数, number_in is 100
120

in test_in 函数, number_in is 200
220

 3.闭包再理解

  1)计算 y = a* x +b 的值

 def test(a,b):
     #计算 y = a*x+b   
     def test_in(x):
         print(a*x+b)
 
     return test_in
 
 line1 = test(1,2)
 line1(1)
 
 line2 = test(2,10)
 line2(3)

     

vim  替换

:%s///g    

:%s/^/#/g

:1,7s/^/#/g         1-7行^替换为#  

  2)版本2:旧方法:

 def test(a,b,x):
     return (a*x+b)
 
 a=1
 b=2
 c=3
 result = test(a,b,c)
 result1 = test(a,b,c)
 print(result)
 print(result1)

这个例子中,函数line与变量a,b构成闭包。在创建闭包的时候,我们通过line_conf的参数a,b说明了这两个变量的取值,这样,我们就确定了函数的最终形式(y = x + 1和y = 4x + 5)。我们只需要变换参数a,b,就可以获得不同的直线表达函数。由此,我们可以看到,闭包也具有提高代码可复用性的作用。

如果没有闭包,我们需要每次创建直线函数的时候同时说明a,b,x。这样,我们就需要更多的参数传递,也减少了代码的可移植性。

4.闭包思考

闭包思考:

1.闭包似优化了变量,原来需要类对象完成的工作,闭包也可以完成
2.由于闭包引用了外部函数的局部变量,则外部函数的局部变量没有及时释放,消耗内存
原文地址:https://www.cnblogs.com/venicid/p/7928199.html