# python3 第二十一章

`def calc_sum(*args): ax = 0 for n in args: ax = ax + n return ax`

`def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sumf = lazy_sum(1, 2, 3, 4)print(f)print(f())`

`<function lazy_sum.<locals>.sum at 0x1014476a8>10`

`def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sumf1 = lazy_sum(1, 2, 3, 4)f2 = lazy_sum(1, 2, 3, 4)print('f1==f2 :', f1 == f2)`

`f1==f2 : False`

f1()和f2()的调用结果互不影响。

`def line_conf(a, b): def line(x): return a * x + b return lineline1 = line_conf(1, 2)line2 = line_conf(3, 7)print(line1(5), line2(5))`

`def count(): fs = [] for i in range(1, 4): def f(): return i*i fs.append(f) return fsf1, f2, f3 = count()print('f1():', f1())print('f2():', f2())print('f3():', f3())`

`f1(): 9f2(): 9f3(): 9`

`def count(): def f(j): def g(): return j * j return g fs = [] for i in range(1, 4): fs.append(f(i)) # f(i)立刻被执行，因此i的当前值被传入f() return fsf1, f2, f3 = count()print('f1():', f1())print('f2():', f2())print('f3():', f3())`

`f1(): 1f2(): 4f3(): 9`