[关闭]
@Scrazy 2016-02-15T08:38:06.000000Z 字数 273 阅读 738

闭包


python学习笔记


闭包的两个例子


ex1:

  1. def line_conf():
  2. def line(x):
  3. return x ** 2 + 1
  4. return line
  5. my_line = line_conf()
  6. print(my_line(5))
  7. 运行结果
  8. 26
  1. list(map(lambda x: x ** 2 + 1, [5]))
  2. 运行结果
  3. 26

ex2:

  1. def line_conf(a, b):
  2. def line(x):
  3. return a * x + b
  4. return line
  5. line1 = line_conf(1, 1)
  6. line2 = line_conf(4, 5)
  7. print(line1(6), line2(6))
  8. 运行结果
  9. 7 29
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注