1. 高阶函数定义
    1.函数接收的参数是一个函数名
    2.函数的返回值是一个函数名
    以上两者满足任意一个,就是高阶函数
  2.  
  3. 装饰器定义
    本质就是函数,功能是为其他函数添加新功能
  1. 装饰器的原则

1.不修改被装饰函数的源代码(开放封闭原则)

2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式

装饰器=高阶函数+函数嵌套+闭包

   

  1. # 无返回值无参数
  2. import time
  3.  
  4. def timer(func): #func = test
  5. def w():
  6. start_time = time.time()
  7. func() #就是在运行test()
  8. stop_time = time.time()
  9. print("运行时间是%d"%(stop_time-start_time))
  10. return w
  11.  
  12. @timer # 相当于 test = timer(test)
  13. def test():
  14. time.sleep(2)
  15. print("from test")
  16.  
  17. # test = timer(test) #返回的是w的地址
  18. # test() #相当于执行w
  19.  
  20. test()
  1. #加上返回值
  2. import time
  3.  
  4. def timer(func): #func = test
  5. def w():
  6. start_time = time.time()
  7. res = func() #就是在运行test()
  8. stop_time = time.time()
  9. print("运行时间是%d"%(stop_time-start_time))
  10. return res
  11. return w
  12.  
  13. @timer # 相当于 test = timer(test)
  14. def test():
  15. time.sleep(2)
  16. print("from test")
  17. return "这是test的返回值"
  18.  
  19. # test = timer(test) #返回的是w的地址
  20. # test() #相当于执行w
  21.  
  22. res = test()
  23. print(res)
  1. #加上参数和返回值 装饰器最终形式
  2. import time
  3.  
  4. def timer(func): #func = test #func = test1
  5. def w(*args,**kwargs):
  6. start_time = time.time()
  7. res = func(*args,**kwargs) #就是在运行test() test1()
  8. stop_time = time.time()
  9. print("运行时间是%d"%(stop_time-start_time))
  10. return res
  11. return w
  12.  
  13. @timer # 相当于 test = timer(test)
  14. def test(name,age):
  15. time.sleep(2)
  16. print("from test %s %s"%(name,age))
  17. return "这是test的返回值"
  18.  
  19. res = test("liao",18)
  20. print(res)
  21.  
  22. @timer # 相当于 test1 = timer(test1)
  23. def test1(name,age,g):
  24. time.sleep(1)
  25. print("from test1 %s %s %s"%(name,age,g))
  26. return "这是test1的返回值"
  27.  
  28. res1 = test1("bo",26,"shi")
  29. print(res1)

用户登陆(简单流程判断)

  1. l = [{"name":"liao","pwd":"123"},{"name":"tom","pwd":"123"}] #用户数据
  2. c_d = {"user":None,"login":False} #定义一个空的临时的用户字字典
  3.  
  4. def a_f(func):
  5. def w(*args,**kwargs):
  6. if c_d["user"] and c_d["login"]: #判断临时字典里是否有用户登陆,没有就输入
  7. res = func(*args,**kwargs) #有就进入下一步
  8. return res
  9.  
  10. user = input("请输入用户名:").strip() #临时字典没有数据就输入用户名
  11. pwd = input("请输入密码:").strip() #临时字典没有数据就输入密码
  12.  
  13. for i in l: #遍历用户数据
  14. if user == i["name"] and pwd == i["pwd"]: #判断输入的用户和密码是否在用户数据里
  15. c_d["user"] = user #输入正确,数据保存到临时的用户字字典里,下一步不用再输入用户和密码
  16. c_d["login"] = True
  17. res = func(*args,**kwargs) #进入
  18. return res
  19. else: #如果输入的用户名和密码不在用记数据里,提示用户
  20. print("用户名或者密码错误")
  21. return w
  22.  
  23. @a_f
  24. def index():
  25. print("欢迎来到主页面")
  26.  
  27. @a_f
  28. def home():
  29. print("这里是你家")
  30.  
  31. @a_f
  32. def shopping_car():
  33. print("查看购物车啊亲")
  34.  
  35. index()
  36. home()
  37. shopping_car()
  1.  
  1.  

python 高阶函数与装饰器的更多相关文章

  1. Python高阶函数之 - 装饰器

    高阶函数:  1. 函数名可以作为参数传入     2. 函数名可以作为返回值. python装饰器是用于拓展原来函数功能的一种函数 , 这个函数的特殊之处在于它的返回值也是一个函数 , 使用pyth ...

  2. Python学习笔记【第六篇】:迭代器、生成器、高阶函数、装饰器

    迭代器 迭代器是访问集合元素的一种方式,迭代器从对象的第一个元素开始访问,知道所有元素被访问完成.迭代器只能往前访问,不能通过索引访问. 类型内部使用__iter__()方法转为迭代器,使用__nex ...

  3. python开发基础04-函数、递归、匿名函数、高阶函数、装饰器

    匿名函数 lamba lambda x,y,z=1:x+y+z 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函 ...

  4. python笔记十三(高阶函数、装饰器)

    一.高阶函数 函数只要有以下两个特征中一个就可以称为高阶函数: a:函数名作为一个实参传入另一个函数中 b:函数的返回值中包含函数名 下面我们用代码来感受一下这两种形式: import time # ...

  5. Python_高阶函数、装饰器(decorator)

    一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...

  6. python中高阶函数与装饰器

    高阶函数的定义:传入参数有函数名或者返回值有内置函数名的函数. 最简单的高阶函数: def add(x, y, f):    return f(x) + f(y) add(-5, 6, abs) 常用 ...

  7. Python3(十) 函数式编程: 匿名函数、高阶函数、装饰器

    一.匿名函数 1.定义:定义函数的时候不需要定义函数名 2.具体例子: #普通函数 def add(x,y): return x + y #匿名函数 lambda x,y: x + y 调用匿名函数: ...

  8. Python(十) 函数式编程: 匿名函数、高阶函数、装饰器

    一.lambda表达式 lambda parameter_list: expression # 匿名函数 def add(x,y): return x+y print(add(1,2)) f = la ...

  9. python中高阶函数与装饰器(3)

    >>> f = lambda x: x * x>>> f<function <lambda> at 0x101c6ef28> >> ...

随机推荐

  1. Oracle Commit 方式 COMMIT WRITE batch NOWAIT;

    1111 CREATE OR REPLACE PROCEDURE update_hav_tpnd IS  CURSOR hav_tpnd_cur IS    SELECT d.hav_tpnd, d. ...

  2. SQL 之witn as语法

    with as 是临时视图的语法:with qry_a as (select * from table_a )select * from qry_a ;

  3. [Appium] 使用Appium过程中遇到的各种坑

    以下问题都是以ios为背景: 1. 问题: Case: 在页面S1上,点击元素A后,判读B元素是否出现. Detail:一开始通过Appium Inspector, 可以找到B元素,所以直接取该元素的 ...

  4. EXT学习之——EXT下拉框默认绑定第一个值

    //默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...

  5. Ubuntu 12.10使用apt安装Oracle/Sun JDK

    apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java sudo apt-get ...

  6. 【MySQL】MySQL 5.7+ 版本的初始化

    MySQL 5.7.7以上二进制包就不包括原data目录的初始化系统表,官网说明: http://dev.mysql.com/doc/refman/5.7/en/data-directory-init ...

  7. PIC32MZ tutorial -- Change Notification

    In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...

  8. node,不懂不懂

    Four Day-------------------------node.js分对象全局/核心模块/文件模块path(核心模块)--作用:操作路径basername/获取传入路劲dimame/获取传 ...

  9. A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...

  10. linux C学习笔记02--共享内存(进程同步)

    system V下3中进程同步:共享内存(shared memory),信号量(semaphore)和消息队列(message queue) 调试了下午,终于调通啦! 运行./c.out 输出共享内存 ...