lambda
names = ['a', 'b', 'c']def test_fun(name, x): print(name, x)def gen_clousure(name): return lambda x: test_fun(name, x)funcs1 = [gen_clousure(n) for n in names]funcs2 = [lambda x: test_fun(n, x) for n in names]# this is what I wantIn [88]: for f in funcs1: ....: f(1)a 1b 1c 1# I do not understand why I get thisIn [89]: for f in funcs2: ....: f(1) c 1 c 1 c 1
相关分类