闭包
闭包的定义:
在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称为闭包。
1 def test(number): 2 print("...1...") 3 def test_in(number2): 4 print("...2...") 5 print(number+number2) 6 print("...3...") 7 return test_in #返回的函数的引用 8 9 ret = test(100) #实际ret等于test函数的额返回值:ret = test_in 10 print("*"*30) 11 ret(1)#等于test_in()
闭包的应用:
1 def test(a,b): 2 def test_in(x): 3 print(a*x+b) 4 return test_in 5 6 line1 = test(1,1) 7 line1(0) 8 line2 = test(10,4) 9 line2(0)

浙公网安备 33010602011771号