1. 定义:装饰器本质是函数,(装饰其他函数)就是为其他函数添加附加功能
    原则:1、不能修改被装饰的函数的源代码
    2、不能修改装饰的函数的调用方式
  2.  
  3. 实现装饰器知识储备
    1函数即变量
    2、高阶函数,满足2个条件之一 1、把一个函数名当做实参传给另外一个函数。2、返回值中包含函数名
    1、在不修改被装饰的函数的源代码的情况下,为其添加功能 2、不能修改函数的调用方式)
    3、嵌套函数
    高阶函数+嵌套函数=》装饰器
    python内存回收机制,匿名函授和变量名没有的情况下会被回收
  1. def foo():
  2. print('in the foo')
  3. bar()
  4. def bar():
  5. print('in the bar')
  6. foo()
  7. import time
  8.  
  9. def bar():
  10. time.sleep(0.5)
  11. print('in the bar')
  12.  
  13. def test1(func):
  14. start_time=time.time()
  15. print(func)
  16. time.sleep(0.5)
  17. func()
  18. stop_time=time.time()
  19. print("the func run time is %s"%(stop_time-start_time))
  20.  
  21. test1(bar)#打印内存地址,调用方式改变了
  22.  
  23. import time
  24.  
  25. def bar():#定义原始函数
  26. time.sleep(0.5)#停止0.5
  27. print('in the bar')#定义过程
  28. def test2(func):#定义装饰器函数
  29. print(func)#定义过程
  30. return func#返回值“func”
  31. print(test2(bar))#打印函数test2bar中的内存地址
  32. bar=test2(bar)#重新定义变量bar=函数test2bar为实参
  33. bar()#run bar#运行bar

  

  1. 嵌套函数:在一个函数的体内用def去声明一个函数
  1. def foo():
  2. print('in the foo')
  3. def bar():#在一个函数的体内用def去声明一个函数
  4. print('in the bar')
  5.  
  6. bar()
  7. foo()
  8. x=0
  9. def gramdpa():
  10. x=1
  11. def dad():
  12. x=2
  13. def son():
  14. x=3
  15. print(x)
  16. son()
  17. dad()
  18. gramdpa()
  19.  
  20. import time

  

  1. 装饰器1——>无参数装饰器(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
  1. def timer(func):#定义一个名为timer的函数,参数为functimer(test1)把test1的内存地址传给了func
  2. def deco():#声明一个新的函数deco,函数的嵌套,他的作用
  3. start_time=time.time()#开始时间
  4. func()#等于运行run test1 适用无参数的源代码
  5. stop_time=time.time()#结束时间
  6. print('the func run time is %s'%(stop_time-start_time))#打印...
  7. return deco#高阶函数返回值中有函数deco内存地址
  8.  
  9. @timer
  10. #源代码
  11. def test1(): #定义名为test1函数
  12. time.sleep(1)#等3
  13. print('in the test1')#函数过程 打印...
  14. print(timer(test1))
  15. test1=timer(test1)
  16. test1()#--->执行deco

  

  1. 装饰器2-->含多个参数(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
  1. import time
  2. def timer(func):#定义一个名为timer的函数,参数为functimer(test1)把test1的内存地址传给了func
  3. def deco(*args,**kwargs):#声明一个新的函数deco,函数的嵌套,他的作用
  4. start_time=time.time()#开始时间
  5. func(*args,**kwargs)#等于run test1 *args,**kwargs适用任何参数的源代码
  6. stop_time=time.time()#结束时间
  7. print('the func run time is %s'%(stop_time-start_time))#打印...
  8. return deco#高阶函数返回值中有函数deco内存地址
  9.  
  10. @timer
  11. 源代码
  12. def test1(): #定义名为test1函数
  13. time.sleep(1)#等3
  14. print('in the test1')#函数过程 打印...
  15. print(timer(test1))
  16. test1=timer(test1)
  17. test1()#--->执行deco
  18.  
  19. @timer
  20. def test2(name,age):#定义名为test2函数,含有name参数
  21. print("test2:",name,age)#打印 ...
  22.  
  23. test1()
  24. test2("dream",22)#对应源代码加入参数才能运行

  

  1. 装饰器3--输入用户名密码,(源代码没有返回数据的装饰器)
  1. import time
  2. user,passwd='dream','133456'#默认密码
  3. def auth(func):
  4. def wrapper(*args,**kwargs):
  5. print("wrapper func args:",*args,**kwargs)
  6. username=input("Username:").strip()#移除空格和回车字符
  7. password=input("Password:").strip()
  8. if user==username and passwd==password:#验证用户名和密码
  9. print('\033[32;1mUser has passed authentication\033[0m')
  10. func(*args,**kwargs)#执行源代码
  11. else:
  12. exit('\33[31;1mInvalid username or password\033[0m')#否则退出
  13. return wrapper
  14.  
  15. @auth
  16. def index():
  17. print('welcome to index page')
  18. @auth
  19. def home():
  20. print("welcome to home page")
  21.  
  22. @auth
  23. def bbs():
  24. print("welcome to home page")
  25.  
  26. # index()
  27. # home()
  28. # bbs()

  

  1.  
  2. #装饰器4--输入用户名密码,源代码含有返回数据的装饰器
  1. import time
  2. user,passwd='dream','133456'#默认密码
  3. def auth(func):
  4. def wrapper(*args,**kwargs):
  5. # print("wrapper func args:",*args,**kwargs)
  6. username=input("Username:").strip()#移除空格和回车字符
  7. password=input("Password:").strip()
  8. if user==username and passwd==password:#验证用户名和密码
  9. print('\033[32;1mUser has passed authentication\033[0m')
  10. res=func(*args,**kwargs)#定义一个变量
  11. return res
  12.  
  13. else:
  14. exit('\33[31;1mInvalid username or password\033[0m')#否则退出
  15. return wrapper
  16.  
  17. @auth
  18. def index():
  19. print('welcome to index page')
  20. @auth
  21. def home():
  22. print("welcome to home page")
  23. return "from home"
  24.  
  25. @auth
  26. def bbs():
  27. print("welcome to home page")
  28.  
  29. # #index()
  30. # print(home())
  31. # #bbs()

  

  1. 装饰器5--输入用户名密码,判断本地认证和远程认证
  1. import time
  2. user,passwd='dream','133456'#默认密码
  3. def auth(auth_type):
  4. print("auth func:",auth_type)
  5. def outer_wrapper(func):
  6. def wrapper(*args,**kwargs):#定义wrapper函数带有两个函数
  7. print("wrapper func args:",*args,**kwargs)
  8. if auth_type=='local':
  9. username=input("Username:").strip()#移除空格和回车字符
  10. password=input("Password:").strip()
  11. if user==username and passwd==password:#验证用户名和密码
  12. print('\033[32;1mUser has passed authentication\033[0m')
  13. res=func(*args,**kwargs)#执行源代码
  14. print('---after authenticaion')
  15. return res
  16. else:
  17. exit('\33[31;1mInvalid username or password\033[0m')#否则退出
  18.  
  19. elif auth_type=="ldap":
  20. print("行不通")
  21. return wrapper
  22.  
  23. return outer_wrapper
  24.  
  25. def index():
  26. print('welcome to index page')
  27. @auth(auth_type="local")#本地认证
  28. def home():
  29. print("welcome to home page")
  30. return "from home"
  31. @auth(auth_type="ldap") #远程认证
  32. def bbs():
  33. print("welcome to home page")
  34. #
  35. #index()
  36. home()
  37. bbs()

  

  1.  
  1.  

学习python第十三天,函数5 装饰器decorator的更多相关文章

  1. Python学习第四十天函数的装饰器用法

    在软件开发的过程中,要遵循软件的一些原则封装的,不改变原有的代码的基础增加一些需求,python提供了装饰器来扩展函数功能,下面说说函数装饰器用法 def debug(func):      def ...

  2. Python菜鸟之路:Python基础-逼格提升利器:装饰器Decorator

    一.装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身 ...

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

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

  4. Python基础(闭包函数、装饰器、模块和包)

    闭包函数 格式: def 函数名1(): def 函数名2(): 变量 = 值 return 变量 return 函数名2 func = 函数名1() key = func()

  5. python 基础篇 11 函数进阶----装饰器

    11. 前⽅⾼能-装饰器初识本节主要内容:1. 函数名的运⽤, 第⼀类对象2. 闭包3. 装饰器初识 一:函数名的运用: 函数名是一个变量,但他是一个特殊变量,加上括号可以执行函数. ⼆. 闭包什么是 ...

  6. Python基础(7)闭包函数、装饰器

    一.闭包函数 闭包函数:1.函数内部定义函数,成为内部函数, 2.改内部函数包含对外部作用域,而不是对全局作用域名字的引用 那么该内部函数成为闭包函数 #最简单的无参闭包函数 def func1() ...

  7. python基础之闭包函数和装饰器

    补充:全局变量声明及局部变量引用 python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 global关键字用来在函数或其 ...

  8. python之集合,函数,装饰器

    本节主要内容如下: 1. set集合 2. 函数 -- 自定义函数 -- 内置函数 3. 装饰器 一. set 集合: 一个无序且不重复的序列. tuple算是list和str的杂合(杂交的都有自己的 ...

  9. python基础之闭包函数与装饰器

    闭包函数: 什么是闭包函数: 闭指的是定义在一个函数内部 包指的是该函数包含对外部作用域(非全局作用域)名字的引用 def counter(): n=0 def incr(): nonlocal n ...

随机推荐

  1. .NET开发人员必知的八个网站

    当前全球有数百万的开发人员在使用微软的.NET技术.如果你是其中之一,或者想要成为其中之一的话,我下面将要列出的每一个站点都应该是你的最爱,都应该收藏到书签中去.对于不熟悉.NET技术的朋友,需要说明 ...

  2. Oracle数据库分割字符串function方法

    下面我直接上传一串代码源码, create or replace function strsplit(p_value varchar2, p_split varchar2 := ',') --usag ...

  3. Spring框架中stopwatch(秒表)

    StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下: long startTime = System.currentTimeMillis(); // 你的业务代码 long ...

  4. Spring课程 Spring入门篇 4-1 Spring bean装配(下)之bean定义及作用域注解实现

    课程链接: 1 概述 2 代码演练 3 代码解析 1 概述 1.1 bean注解相关 a context:component-scan标签使用 问:该标签的作用是什么? 答:该标签作用是支持注解,在x ...

  5. ActiveMQ实例2--Spring JMS发送消息

    参考文章:http://my.oschina.net/xiaoxishan/blog/381209#OSC_h3_7 一,步骤参照参考文献 二.新建的项目 三.补充 web.xml <?xml ...

  6. Android实现异步的几种方法

    在Android项目中,有经验的开发人员都知道,一些耗时的IO操作等都必须在子线程中去操作,那么可以有哪些方法来开启子线程呢,一般可以使用Java中自带的几种方法,也可以使用Andorid特有的一些类 ...

  7. [垂直化搜索引擎]lucene简介及使用

    摘自:大型分布式网站架构-设计与实践

  8. ssh-agent && ssh-agent forward && SSH ProxyCommand

    http://blog.csdn.net/sdcxyz/article/details/41487897 https://developer.github.com/guides/using-ssh-a ...

  9. VirtualBox虚拟机 host/guest 拷贝粘贴,共享剪贴板,安装guest additions

    Oracle VirtualBox 虚拟机,为了在主机.从机间拷贝文件,共享剪贴板,需要进行设置,以及安装guest additions软件 测试环境 host: windows 7 professi ...

  10. zendstudio 汉化

    http://archive.eclipse.org/technology/babel/index.php http://www.eclipse.org/babel/downloads.php 注册码 ...