python中的decorator的作用
1、概念
装饰器(decorator)就是:定义了一个函数,想在运行时动态增加功能,又不想改动函数本身的代码。可以起到复用代码的功能,避免每个函数重复性编写代码,简言之就是拓展原来函数功能的一种函数。在python中,装饰器(decorator)分为函数装饰器和类装饰器两种。python中内置的@语言就是为了简化装饰器调用。
列出几个装饰器函数:
打印日志:@log
检测性能:@performance
数据库事务:@transaction
URL路由:@post('/register')
2、使用方法
(1)无参数decorator
编写一个@performance,它可以打印出函数调用的时间。
import time def performance(f):
def log_time(x):
t1 = time.time()
res = f(x)
t2 = time.time()
print 'call %s() in %fs' %(f.__name__,(t2 - t1))
return res
return log_time @performance
def factorial(n):
return reduce(lambda x,y : x*y,range(1,n+1)) print factorial(10)
运行结果:
call factorial() in 0.006009s 3628800
运行原理:
此时,factorial就作为performance的函数对象,传递给f。当调用factorial(10)的时候也就是调用log_time(10)函数,而在log_time函数内部,又调用了f,这就造成了装饰器的效果。说明f是被装饰函数,而x是被装饰函数的参数。
(2)带参数decorator
请给 @performace 增加一个参数,允许传入's'或'ms'。
import time def performance(unit):
def perf_decorator(f):
def wrapper(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
t = (t2 - t1)*1000 if unit =='ms' else (t2 - t1)
print 'call %s() in %f %s'%(f.__name__, t, unit)
return r
return wrapper
return perf_decorator @performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1)) print factorial(10)
运行结果:
call factorial() in 9.381056 ms 3628800
运行原理:
它的内部逻辑为factorial=performance('ms')(factorial);
这里面performance('ms')返回是perf_decorator函数对象,performance('ms')(factorial)其实就是perf_decorator(factorial),然后其余的就和上面是一样的道理了。
python中的decorator的作用的更多相关文章
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- 【转】浅析python 中__name__ = '__main__' 的作用
原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...
- 002_浅析python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- python中的双冒号作用
Python序列切片地址可以写为[开始:结束:步长],其中的开始和结束可以省略. 1. range(n)生成[0,n)区间整数 2. 开始start省略时,默认从第0项开始 3. 结尾省略的时候,默认 ...
- 理解 python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- python 中__name__ = '__main__' 的作用,到底干嘛的?
python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...
- python 中的decorator
python 中decorator的作用就是一个包装的作用,所谓包装指在执行真正的函数之前或者之后,我们可以有一些额外的发挥余地. decorator形式如下 def dec(arg1): print ...
- FAQ: Python中if __name__ == '__main__':作用
#hello.pydef sayHello(): str="hello" print(str); if __name__ == "__main__": prin ...
- Python中的pass的作用
1.pass语句什么也不做,一般作为占位符或者创建占位程序,pass语句不会执行任何操作2.保证格式完整 3.保证语义完整 以if语句为例,在c或c++/java中: ? if(true) ;//do ...
随机推荐
- (十一)SpringBoot导出excel文件
一:添加POI依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oox ...
- HTTP1.1规范下载由6个文档组成
- 黑马Stream流学习 Stream流 函数式接口 Lambda表达式 方法引用
- go系列(5)- beego自己写controller
前边的系列文章已经讲述了如何安装环境, beego的处理逻辑都是在Controller里面完成的,下面就写一个最简单的Controller. 我们在写自己的controller的时候,一定要继承bee ...
- Zynq7000开发系列-5(OpenCV开发环境搭建:Ubuntu、Zynq)
操作系统:Ubuntu14.04.5 LTS 64bit OpenCV:OpenCV 3.1.0.opencv_contrib gcc:gcc version 4.8.4 (Ubuntu 4.8.4- ...
- HDU6299(2018多校第一场)
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6299 两个字符串的排序可以分成四种情况: (1)str1左少右多 vs str2 左多右 ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- CVE-2012-0002(MS12-020)3389远程溢出漏洞
1:攻击机:Kali和windows皆可 2:目标机:windows XP系统(开启3389) Kali测试: search m12-020 搜索ms12-020 use auxiliary/dos/ ...
- 一张图告诉你,只会NodeJS还远远不够!
NodeJS看似小巧简单,却威力无边,一张图,秒懂!!! 可能很多人还不会安装,但至少已经会了javascript,或者至少会了jquery,那么js还可以干更多的事情!js还可以干更多的事情!js还 ...
- Linux下环境搭建(三)——jmeter+ant配置
在linux环境下,使用jmeter做接口自动化,做好了前两步的准备工作后,怎能少了主角jmeter+ant了,今天就来说下jmeter+ant的配置方式. jmeter配置 jmeter下载地址:h ...