美文网首页
Python 函数式编程

Python 函数式编程

作者: meltra | 来源:发表于2017-03-30 21:31 被阅读0次

    Map()

      def f(x):
        return x*x
      print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
    

    Reduce()

    from functools import reduce
    
      def f(x, y):
        return x + y
      reduce(f,[1,3,5,7,9],100)     //从100为初始数
    

    filter()

      def is_odd(x):
        return x % 2 == 1
      filter(is_odd,range(1,101))
    

    闭包

    内层函数引用了外层函数的变量,然后返回内层函数的情况,称为闭包(Closure)。

    闭包的特点是 返回的函数还引用了外层函数的局部变量,所以,要正确使用闭包,就要确保引用的局部变量在函数返回后不能变。

    匿名函数

      lambda x:x*x
    

    相关文章

      网友评论

          本文标题:Python 函数式编程

          本文链接:https://www.haomeiwen.com/subject/xxgpottx.html