zoukankan      html  css  js  c++  java
  • Python入门学习笔记10:函数式编程:匿名函数、高阶函数、装饰器

      1 #函数式编程:匿名函数、高阶函数、装饰器
      2 
      3 #匿名函数
      4 
      5 
      6 def add(x,y):#常规函数
      7     return x+y
      8 
      9 
     10 print(add(1,2))
     11 
     12 f = lambda x,y: x+y#匿名函数
     13 print(f(1,2))
     14 
     15 # lambada 表达式
     16 # lambada parameter_list : expression
     17 
     18 
     19 # 三元表达式
     20 
     21 # x,y x 大于 y   x 否则 y   常规写法 x > y ? x :y
     22 # 条件为真时返回的结果 if 条件判断 else 条件为假时的返回结果
     23 x = 1
     24 y = 3
     25 r = x if x > y else y
     26 print(r)
     27 
     28 
     29 # map 方法与for循环类似
     30 list_x = [1,2,3,4,5,6,7,8]
     31 list_y = [1, 4, 9, 16, 25, 36, 49, 64]
     32 
     33 
     34 def square(x):
     35     return x * x
     36 
     37 
     38 # for x in list_x:
     39 #     square(x)
     40 #     print(square(x))
     41 
     42 r = map(square,list_x)
     43 print(list(r))
     44 
     45 #匿名函数代替square表达式
     46 list_x1 = [1,2,3,4,5,6,7,8]
     47 list_y1 = [1,2,3,4,5,6,7,8]
     48 r1 = map(lambda x:x*x,list_x1)
     49 print(list(r1))
     50 # map函数可传入多个列表 多个列表进行计算
     51 r2 = map(lambda x,y:x*y,list_x1,list_y1)
     52 print(list(r2))
     53 
     54 # reduce 连续计算,连续调用lambada(单列表进行连续计算)
     55 from functools import reduce
     56 list_x1_str = ['1','2','3','4','5','6','7','8']
     57 r3 = reduce(lambda x,y:x+y,list_x1_str,'aaa')  #根据list_x1数组在reduce方法中使用匿名函数的算法为:第一次数组第一个和第二个元素作为x,y,第二次取上一数组的值和第三个元素作为新的x,y进行运算,依此类推
     58 print(r3)
     59 # map/reduce 在大数据中作为 编程模型 映射 归约 并行计算 函数式编程
     60 # filter 过滤器
     61 list_filter = [1,0,1,0,0,1]
     62 list_filter_u = ['a','B','c','F']
     63 r5 = filter(lambda x: True if x==1 else False,list_filter)
     64 r6 = filter(lambda x : x,list_filter)
     65 print(list(r5),list(r6))
     66 
     67 #命令式编程
     68 # map/reduce/filter/lambda
     69 
     70 #函数式编程
     71 #def/if else/for
     72 #lisp 函数式编程的鼻祖(人工智能领域用的比较多)
     73 
     74 #装饰器
     75 #装饰器在其他语言中的应用:Python:装饰器 C#:特性 Java:注解
     76 
     77 import time
     78 
     79 
     80 def f1():
     81     #print(time.time())
     82     print('This is a function1')
     83 
     84 
     85 #f1()
     86 
     87 
     88 def f2():
     89     print('This is a function2')
     90 
     91 
     92 def print_current_time(func):
     93     print(time.time())
     94     func()
     95 
     96 
     97 """
     98 等价于
     99 print(time.time())
    100 f1()
    101 print(time.time())
    102 f2()
    103 """
    104 
    105 
    106 print_current_time(f1)
    107 print_current_time(f2)
    108 
    109 
    110 # 装饰器
    111 
    112 
    113 def decorator(func1):
    114     def wrapper(*args,**kwargs):#kwargs关键字参数  key word aygs
    115         print(time.time())
    116         func1(*args,**kwargs)
    117     return wrapper
    118 
    119 
    120 @decorator
    121 def f3(func_name):
    122     #print(time.time())
    123     print('This is a function3' + func_name)
    124 #等价于 f3 = decorator(f3)
    125 
    126 
    127 @decorator
    128 def f4(func_name1,func_name2):
    129     #print(time.time())
    130     print('This is a function4' + func_name1,func_name2)
    131 
    132 #@decorator
    133 def f5(func_name1,func_name2,**kwargs):
    134     #print(time.time())
    135     print('This is a function4' + func_name1,func_name2,kwargs)
    136 
    137 
    138 f3('test func')
    139 f4('name1','name2')
    140 f5('name1','name2',a=1,b=2,c='123')
  • 相关阅读:
    打包.a 文件时, build phases- Link Binary With Libraries
    Undefined symbols for architecture i386: "_deflate", referenced from:
    iOS 9 failed for URL: "XXX://@"
    ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
    ASP.NET中利用DataList实现图片无缝滚动
    老码农教你在 StackOverflow 上谈笑风生
    ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
    Asp.net面试题
    Asp.Net之后台加载JS和CSS
    .net环境下从PDF文档中抽取Text文本的一些方法汇总
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/13355953.html
Copyright © 2011-2022 走看看