1.装饰器:本质是函数,是用来给其他函数添加附加扩展功能的函数,其返回值是一个函数(函数指针)

2.装饰器作用:不改变函数源代码和函数调用方式的前提下添加函数的附加功能。

3.装饰器储备知识点:

  A.高阶函数

  B.嵌套函数(闭包函数)

  C.“函数即变量”

4.装饰器函数的外部函数传入要装饰的函数名字,返回经过修饰后函数的名字;内层函数(闭包)负责修饰被修饰函数。

    实质: 是一个函数

    参数:是你要装饰的函数名(并非函数调用

    返回:是装饰好的函数名(也非函数调用

    作用:为已经存在的对象添加额外的功能

    特点:不需要对对象做任何的代码上的变动

5.装饰器分类:例子

  A.一般装饰器(函数内调用另一个函数)

   


import time
def decorator(function): #计算出函数function运行时间
start_time=time.time()
function()
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time) def test(): #测试函数
print('in the test') decorator(test) #调用装饰器 执行结果如下:

  B.简单装饰器

import time
def decorator(function): #计算出函数function运行时间
start_time=time.time()
function()
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time)
@decorator #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
def test(): #测试函数
print('in the test') test #调用被装饰函数 执行结果如下:

3.被装饰函数带一个参数的装饰器



import time
def decorator1(function): #计算出函数function运行时间
def decorator2(x): #参数代入
start_time=time.time()
function(x) #参数代入,此处参数名必须与上一步参数名一样
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time)
return decorator2 #返回decorator,此处人的理解是调用decorator1返回decorator2的引用 @decorator1 #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
def test(args): #测试函数
print('in the test',args) test("argument") #调用被装饰函数,此处实参为字符串argument 执行结果:

4.被装饰函数带多个或者不确定参数个数的装饰器:此处回顾函数参数调用的内容(*args,**kwargs)

import time
def decorator1(function): #计算出函数function运行时间
def decorator2(*args,**kwargs): #参数代入
start_time=time.time()
function(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time)
return decorator2 #返回decorator,此处本的理解是两个嵌套函数的逐个调用 @decorator1 #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
def test(args1,args2): #测试函数
print('in the test',args1,args2) test('multiple','argument') #调用被装饰函数,此处实参为字符串argument 执行结果:

5.自身带参数的装饰器


import time
def decorator1(args1): #args1作用于装饰器decorator1内部
def decorator2(args2): #
def decorator3(*args,**kwargs): #参数代入
start_time=time.time()
for i in range(args1): #args1作用于此
args2(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time)
return decorator3 #返回decorator3,此处人的理解是返回decorator3的引用
return decorator2 #返回decorator2,此处人的理解是返回decorator2的引用 @decorator1(3) #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
def test(args1,args2): #测试函数
print('in the test',args1,args2) test('multiple','argument') #调用被装饰函数,此处实参为字符串argument 执行结果:
 

6.多个装饰器使用:


import time
def decorator1(x): #args1作用于装饰器decorator1内部
def decorator2(args2): #
def decorator3(*args,**kwargs): #参数代入
start_time=time.time()
for i in range(x): #args1作用于此
args2(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
stop_time=time.time()
run_time=start_time-stop_time
print("function runs %s"%run_time)
return decorator3 #返回decorator3,此处人的理解是返回decorator3的引用
return decorator2 #返回decorator2,此处人的理解是返回decorator2的引用 def decorator4(funct):
def decorator5(*args,**kwargs):
print('-------------decorator4 start-------------')
funct(*args,**kwargs)
print('------------decoratoe4 end here----------')
print('\n')
return decorator5 @decorator1(3) #此处的decorator1装饰了下面的的整体
@decorator4 #此处的decorator4装饰了test
def test(args1,args2): #测试函数
print('in the test',args1,args2) test('multiple','argument') #调用被装饰函数,此处实参为字符串argument

执行结果:

注:A.使用参数时要理解每一个参数代入的量(被装饰函数还是一般参数)

  B.多个装饰器嵌套使用尤其要注意装饰的顺序和层次

###新手小白,请多多指正

python装饰器小计的更多相关文章

  1. Python装饰器小代码

    # coding=utf-8import timedef outer(fun): def inner(): start = time.time() fun() runtime = time.time( ...

  2. python装饰器通俗易懂的解释!

    1.python装饰器 刚刚接触python的装饰器,简直懵逼了,直接不懂什么意思啊有木有,自己都忘了走了多少遍Debug,查了多少遍资料,猜有点点开始明白了.总结了一下解释得比较好的,通俗易懂的来说 ...

  3. Python装饰器由浅入深

    装饰器的功能在很多语言中都有,名字也不尽相同,其实它体现的是一种设计模式,强调的是开放封闭原则,更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也能装饰其他的对象,比如类,但通常,我们 ...

  4. Python装饰器与面向切面编程

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

  5. 一篇关于Python装饰器的博文

    这是一篇关于python装饰器的博文 在学习python的过程中处处受阻,之前的学习中Python的装饰器学习了好几遍也没能真正的弄懂.这一次抓住视频猛啃了一波,就连python大佬讲解装饰器起来也需 ...

  6. python 装饰器 一篇就能讲清楚

    装饰器一直是我们学习python难以理解并且纠结的问题,想要弄明白装饰器,必须理解一下函数式编程概念,并且对python中函数调用语法中的特性有所了解,使用装饰器非常简单,但是写装饰器却很复杂.为了讲 ...

  7. Python 装饰器(Decorator)

    装饰器的语法为 @dec_name ,置于函数定义之前.如: import atexit @atexit.register def goodbye(): print('Goodbye!') print ...

  8. Python装饰器探险

    关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...

  9. Python装饰器的通俗理解

    转载:http://blog.csdn.net/u013471155 在学习Python的过程中,我相信有很多人和我一样,对Python的装饰器一直觉得很困惑,我也是困惑了好久,并通过思考和查阅才能略 ...

随机推荐

  1. 介绍几个好用的android自定义控件

    首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionE ...

  2. 018-继承-OC笔记

    学习目标 1.[掌握]Xcode开发文档 2.[掌握]static关键字 3.[掌握]self关键字 4.[掌握]继承 5.[掌握]NSObject 6.[掌握]访问修饰符 7.[掌握]私有实例变量和 ...

  3. Ext.Net 1.x_Ext.Net.GridPanel嵌套Checkbox

    解决办法:拼接HTML var tplchecked = '<input type="checkbox" {0}>'; var IsChecked = function ...

  4. AndFix使用感想

    AndFix已经使用了一段时间了,但是到AndFix上看了一下,最近2个月都没有更新代码了,有141个issues和3个pull request没人处理,其实AndFix的Contributors就俩 ...

  5. C语言关键字static的绝妙用途

    为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量,在变量类型的前面加上就自动清0了,还有就是加上static关键字的,不管是变量还是关键字, ...

  6. Linux文件与目录管理 - ls, cp, mv

    [root@www ~]# ls [-aAdfFhilnrRSt] 目录名称 [root@www ~]# ls [--color={never,auto,always}] 目录名称 [root@www ...

  7. 摄像头ov2685中关于sensor id 设置的相关的寄存器地址

    OV2685 : CHIP_ID address : 0x300A    default : 0x26 address : 0x300B    default : 0x85 address : 0x3 ...

  8. javascript语言扩展:可迭代对象(4)

    js 1.7中还包含一个数组推导(array comprehension)的特性,如果不在最后介绍它好像显得不怎么完整. 数组推导其实很简单: let a = [x*x for(x in range( ...

  9. Python 3 中的json模块使用

    1. 概述 JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式. Python标准库中的json模块提供了JSON数据的处理功能. Python中一种非常常 ...

  10. intersection of two linked lists.(两个链表交叉的地方)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...