python装饰器
来源:互联网 发布:网络教师招聘兼职 编辑:程序博客网 时间:2024/06/03 06:29
python装饰器
什么是装饰器?
装饰器本质是一个python函数,可以让其他函数在不需要做任何代码啊变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。它经常用于有切面需求的场景,比如:插入日志、性能检测、事物处理、缓存、权限校验等场景。
1、非装饰器时的打印函数打印日志:
import timedef deco(func): print "starttime: %s" % time.time() func() print "endtime:%s" % time.time()def myfunc(): print "start myfunc" print "Test myfunc" print "end my func"def main(): deco(myfunc)
运行后的结果:
starttime: 1502033836.49start myfuncTest myfuncend my funcendtime:1502033836.49
可以看到,直接传入函数参数,可以实现计时(打印)的能力,但是在调用的时候,需要使用deco调用,这样造成的后果是,每次在想使用myfunc的时候,都需要使用deco调用,并且,在已实现的逻辑中,需要改动调用方式。
2、使用装饰器打印:
def deco1(func): def wrapper(): print "starttime: %s" % time.time() func() print "endtime:%s" % time.time() return wrapperdef myfunc(): print "start myfunc" print "Test myfunc" print "end my func"def main(): #deco(myfunc) myfunc1 = deco1(myfunc) myfunc1() passif __name__ == "__main__": main()
可以看到,在这种情况下可以用myfunc1 = deco(myfunc)方式调用,这是上面那种传入函数名称所不能达到的。但是存在的问题是,函数名称会冲突,不得不使用myfunc1方式。
3、使用@语法糖的装饰器:
def deco1(func): def wrapper(): print "starttime: %s" % time.time() func() print "endtime:%s" % time.time() return wrapper@deco1def myfunc(): print "start myfunc" print "Test myfunc" print "end my func"def main(): myfunc()
则使用了@语法糖之后,可以直接使用myfunc进行调用。并且,很好的实现了时间统计、日志打印,这是最简单的装饰器场景了。
4、带固定参数的装饰器:
def deco2(func): def wrapper(a, b, c): print "starttime: %s" % time.time() func(a, b, c) print "endtime:%s" % time.time() return wrapper@deco2def myfunc1(a, b, c=100): print "start myfunc1(a, b )" print "a is %s" % a print "b is %s" % b print "c is %s" % c print "end my func1(a, b )"def main(): myfunc1(1, 2, c=3)
执行结果:
starttime: 1502118568.76start myfunc1(a, b )a is 1b is 2c is 3end my func1(a, b )endtime:1502118568.76
5、对参数数量不确定的函数进行装饰:
def deco3(func): def wrapper(*args, **kargs): print "starttime: %s" % time.time() func(*args, **kargs) print "endtime:%s" % time.time() return wrapper@deco3def myfunc1(a, b, c=100): print "start myfunc1(a, b )" print "a is %s" % a print "b is %s" % b print "c is %s" % c print "end my func1(a, b )"def main(): myfunc1(1, 2, c=3)
针对不同参数的情况,可以看到结果正常执行。
6、装饰器支持函数入参:
def deco4(*args0, **kargs0): def _deco4(func): def wrapper(*args, **kargs): print args0[0] print kargs0["b"] print "starttime: %s" % time.time() func(*args, **kargs) print "endtime:%s" % time.time() return wrapper return _deco4@deco4("x", b="y")def myfunc1(a, b, c=100): print "start myfunc1(a, b )" print "a is %s" % a print "b is %s" % b print "c is %s" % c print "end my func1(a, b )"def main(): myfunc1(1, 2, c=3)
执行后可以看到,函数正常的执行了:
xystarttime: 1502120041.85start myfunc1(a, b )a is 1b is 2c is 3end my func1(a, b )endtime:1502120041.85
7、装饰器入参类:
class TestDeco(): def __init__(self): print "init TestDeco" def info(self): print "Now begin to Test Deco" info = classmethod(info)def deco5(cls): def _deco4(func): def wrapper(*args, **kargs): print "starttime: %s" % time.time() print cls.info() func(*args, **kargs) print "endtime:%s" % time.time() return wrapper return _deco4@deco5(TestDeco)def myfunc1(a, b, c=100): print "start myfunc1(a, b )" print "a is %s" % a print "b is %s" % b print "c is %s" % c print "end my func1(a, b )"def main(): myfunc1(1, 2, c=3)
执行后发现,装饰器正常的入参了类,并且打印了正常的信息:
starttime: 1502120586.49Now begin to Test DecoNonestart myfunc1(a, b )a is 1b is 2c is 3end my func1(a, b )endtime:1502120586.49
阅读全文
0 0
- python装饰器装饰类
- [python]python装饰器
- Python装饰器学习
- Python装饰器
- python 装饰器
- Python装饰器
- python 装饰器 应用
- python 装饰器2
- Python装饰器学习
- python装饰器
- python装饰器
- python 装饰器入门
- python装饰器
- python装饰器
- python 装饰器'@'
- python装饰器
- Python装饰器详解
- python 装饰器
- MYSQL时间函数简单使用
- Hive初探[笔记1]
- Eclipse运行时出现这个错误如何解决
- HDU
- 视图解析器
- python装饰器
- 【H.264/AVC视频编解码技术详解】十六:帧内预测编码的基本原理
- swift中用代码控制显示屏幕的方向
- 集合---Set及其子实现类
- jsp包含文件的两种方式
- 初学docker
- HBase与RDBMS&HDFS对比
- WEB项目中的三种下载方式
- BeanFactory和FactoryBean