形成闭包的条件1.外部函数内定义了内部函数2.外部函数有返回值3.返回的是函数名4.内部函数引用了外部函数的变量格式:def 外部函数(): ... def 内部函数(): ... return 内部函数
def func(): a = 100 def inner_func(): b = 99 print(a, b) return inner_func x = func() x()
闭包传参
1 def func(a, b): 2 c = 10 3 4 def inner_func(): 5 s = a+b+c 6 print("相加后的结果:", s) 7 8 return inner_func 9 10 11 # 调用func 12 ifunc = func(1, 2) # ifunc = inner_func 13 14 # 调用ifunc 15 ifunc()
来源:https://www.cnblogs.com/GumpYan/p/12271478.html