Python 学习笔记 -- 内嵌函数、闭包、匿名函数、高阶函数map、高阶函数filter、高阶函数reduce

  1 #------------------------------内嵌函数------------------------------
  2 #内嵌函数就是在函数内部定义函数
  3 #实例一
  4 print("#------------------------------内嵌函数------------------------------")
  5 def funOutOne():
  6     x = 5
  7     def funIn():
  8         x = 3
  9         print("My funOutOne in FunIn x = ", x)
 10     print("My funOutOne x = ",x)
 11     return funIn
 12 
 13 #调用方法
 14 funOutOne()()
 15 
 16 #实例二
 17 def funOutTwo():
 18     x = 5
 19     def funIn():
 20         x = 6
 21         print("My funOutTwo in FunIn x = ",x)
 22     print("My funOutTwo x = ",x)
 23     return funIn()
 24 
 25 #调用方法
 26 funOutTwo()
 27 
 28 #实例二:如何引用函数内部的变量,使用nonlocal
 29 def funOutThree():
 30     x = 10
 31     y = 20
 32     def funIn():
 33         nonlocal x
 34         x = 18
 35         y = 38
 36         print("My funOutThree in funIn x = ",x,"y = ",y)
 37     funIn()
 38     print("My funOutThree x = ",x,"y = ",y)
 39     return None
 40 
 41 funOutThree()
 42 
 43 #实例四:观察它们的调用
 44 def funOutFour():
 45     def funIn():
 46         print("funIn 后被调用!")
 47     print("funOutFour 先被调用!")
 48     return funIn
 49 
 50 funOutFour()()
 51 print("#--------------------------------------------------------------------")
 52 #--------------------------------------------------------------------
 53 #------------------------内嵌函数的闭包特性--------------------------
 54 print("#------------------------内嵌函数的闭包特性--------------------------")
 55 #当把内嵌函数赋值给一个变量是,若变量没有被销毁,则函数内部的参数没有被回收
 56 def closureFun():
 57     x = 1
 58     def funIn():
 59         nonlocal x
 60         x += 1
 61         return x
 62     return funIn
 63 
 64 a = closureFun()
 65 b = closureFun()
 66 c = closureFun()
 67 
 68 print("a,",a())
 69 print("a,",a())
 70 print("a,",a())
 71 print("b,",b())
 72 print("b,",b())
 73 print("c,",c())
 74 #结论,不同变量之间,指向的对象各不相干!
 75     
 76 print("#--------------------------------------------------------------------")
 77 #--------------------------------------------------------------------
 78 #------------------------匿名函数、高阶函数--------------------------
 79 print("#------------------------匿名函数、高阶函数--------------------------")
 80 #匿名函数 以及高阶函数 map(映射) filter(过滤) reduce(归纳)
 81 
 82 #匿名函数:匿名函数可增加代码的可阅读性、精简代码
 83 #实例一:
 84 a = lambda x, y: x + y
 85 print("匿名函数:",a(5,8))
 86 
 87 #实例二:
 88 b = lambda: [x * x for x in range(10)]
 89 list1 = b()
 90 print("匿名函数:",list1)
 91 
 92 #高阶函数map(映射):其实就是把序列中的每一个值作为参数传递给函数后返回另外一个序列
 93 #实例一:
 94 a = map(lambda x: x * 2,[x for x in range(10)])
 95 print("高阶函数map:",list(a))
 96 
 97 #实例二:
 98 b = map(lambda x : x ** 3,[x for x in range(10,21)])
 99 print("高阶函数map:",list(b))
100 
101 #高阶函数filter(过滤):如果序列中的某一个值转化为bool值,值为False,将会被剔除出该序列
102 #实例一:
103 a = filter(lambda x : x % 2, (1,2,3,4,5,6,7,8))
104 print("高阶函数filter",list(a))
105 #实例二:
106 b = filter(lambda x : x % 2 == 0, (1,2,3,4,5,6,7,8))
107 print("高阶函数filter",list(b))
108 
109 #高阶函数reduce:讲一个序列的值作为参数传递给函数,最终返回为一个值!在调用reduce函数前,要记住导入包
110 from functools import reduce
111 #实例一:
112 a = reduce(lambda x, y: x + y,(x for x in range(6)))
113 print("高阶函数reduce",a)
114 
115 #实例二:
116 b = reduce(lambda x, y: x * y,(1,2,3,4,5))
117 print("高阶函数reduce",b)
118 
119 print("#--------------------------------------------------------------------")
120 #--------------------------------------------------------------------

 

posted @ 2017-12-10 22:25  欧晨曦  阅读(287)  评论(0编辑  收藏  举报