python 闭包
#不是闭包def f1(): a = 10 def f2(): a = 20 print(a) print(a) -- 10 f2() -- 20 print(a) --10 f1()
___
闭包
def f1(): a = 10 def f2(): c = 20 * a return f2 f = f1() print(f.__closure__)
origin=0 def factory(pos): def go(step): nonlocal pos new_pos = pos + step pos = new_pos return new_pos return go t = factory(origin) print(t(2)) --2 print(t(3)) --5 print(t(5)) --10print(origin)--0
相关推荐
yuwinter 2020-10-14
归去来兮 2020-09-18
Ericbig 2020-07-19
chaigang 2020-06-27
yogoma 2020-06-14
Andrewjdw 2020-05-27
jokerdby 2020-05-19
Kingonion 2020-04-23
ELEMENTS爱乐冬雨 2020-04-21
sunlizhen 2020-04-17
LczPtr 2020-04-14
Livis的开发之路 2020-03-11
Airuio 2020-03-06
Livis的开发之路 2020-02-28