深入理解 Python 中的装饰器
装饰器本质上也是函数,接收函数对象来作为参数,并在装饰器的内部来调用接受的函数对象完成相关的函数调用,也可以这样理解 ,为了方便在几个不同函数调用之前或者完成相关的统一操作,注意是完成统一的操作,可以传参数使得装饰器不完全一样,后面会讲到,最重要的应用如工程应用上记录相关的内部接口的流水日志,不同的接口需要统一的样式,所以可以用装饰器来实现,先简单看一下实例:
from time import ctime
def deco(func):
def decorator(*args, **kwargs):
print('[%s] %s() is called'% (ctime(), func.__name__))
return func(*arg, **kwargs)
return decorator
@deco
def foo():
print('Holle Python')
foo()
在如上实例中,定义了一个装饰器,其中参数func 需要函数的对象,返回值是decorator函数,其中decorator函数的返回值正是func 的返回值,该装饰器的功能就是在函数调用之前,打印了函数调用的时间和函数名。
装饰器的使用过程很简单,通过注解@符号标注一下即可,这本质上相当于 foo = deco(foo)的嵌套调用。
这里面,你有遇到了 *args 和 **argkwargs,他们可以组合接收任意函数参数。
装饰器也可以堆叠起来,即对某个函数使用多个装饰器,比如:
from time import ctime
def deco(func):
def decorator1(*args, **kwargs);
print('[%s] %s() is called:'%(ctime(), func.__name__))
return func(*args, **kwargs)
return decorator1
def deco2(func):
def decorator2(*args, **kwargs):
print("[%s] %s() is called" % (ctime(), func.__name__))
return func(*args, **kwargs)
return decorator2
@deco2
@deco1
def foo():
print('Hello Python')
foo()
运行一下,输出如下:
[Fri Jul 21 15:15:53 2017] decorator1() is called
[Fri Jul 21 15:15:53 2017] foo() is called
Hello, Python
是否跟你想的一样?在嵌套调用的过程中。foo = deco2(deco1(foo)),所以先返回deco1(foo)的函数名字即为 的从rator1, 后返回 foo 函数名。
装饰器本身也可以传入参数,使得在统一的过程中带点奇特的色彩,如:
from time import ctime
def deco(tag):
def decorator(func);
def wrapper(*args, **kwargs):
print('[%s] %s() is called, Tag is %s' % (ctime(), func.__name__, tag))
return func(*args, **kw)
return warpper
return decorator
@deco('Python')
def foo():
print('Hello Python')
@deco('java')
def bar():
print('Hello Python')
foo()
bar()
让我们简单的分析一下这个装饰器,deco函数接受的是一个str对象tag,当执行deco(‘Python’)后返回的是decotator函数,此函数需要接受一个函数对象,同时返回wrapper函数,而 wrapper 函数的结果就是func 函数返回的值,说的可能有点绕,但理一下会觉得非常简单。
最后说一下的是,由于加入了装饰器,函数的__name__ 和 __doc__ 等信息都发生了变化:
from time import ctime
def deco(dunc):
def decoraor(*args, **kwargs):
print('[%s] %s() is called'% (ctime(), func.__name__))
return func(*args, **kwargs)
return decotator
@deco
def foo():
print('Hello Python')
foo.__name__
foo.__doc__
from time import ctime def deco(func):
def decorator(*args, **kwargs):
'''decorator for func'''
print('[%s] %s() is called' % (ctime(), func.__name__))
return func(*args, **kwargs)
return decorator @deco
def foo():
'''function: foo'''
print('Hello, Python') foo.__name__
foo.__doc__
由此可见,加入装饰器改变了函数内部的相关属性,如何避免此问题呢?Python中有专门的包来避免这种转换:functools.wraps,实例如下:
from time imort ctime
import functools
def deco(func):
@functools.wraps(func)
def decorator(*args, **kwargs):
print('[%s] %s() is called'% (ctime(), func.__name__))
return func(*args, **kwargs)
return decorator
@deco
def foo():
print('Hello Python')
foo.__name__
foo.__doc__
运行结果如下:
foo
function: foo
这样就保留了原先函数的属性,小编在工作中一般也是加入此功能的。
深入理解 Python 中的装饰器的更多相关文章
- 理解Python中的装饰器
文章先由stackoverflow上面的一个问题引起吧,如果使用如下的代码: @makebold @makeitalic def say(): return "Hello" 打印出 ...
- 理解Python中的装饰器//这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档 ...
- 简单说明Python中的装饰器的用法
简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下 装饰器对与 ...
- 【Python】python中的装饰器——@
对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...
- Python 中实现装饰器时使用 @functools.wraps 的理由
Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过 ...
- 写python中的装饰器
python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...
- python中的装饰器decorator
python中的装饰器 装饰器是为了解决以下描述的问题而产生的方法 我们在已有的函数代码的基础上,想要动态的为这个函数增加功能而又不改变原函数的代码 例如有三个函数: def f1(x): retur ...
- python中@property装饰器的使用
目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...
- 【Python】解析Python中的装饰器
python中的函数也是对象,函数可以被当作变量传递. 装饰器在python中功能非常强大,装饰器允许对原有函数行为进行扩展,而不用硬编码的方式,它提供了一种面向切面的访问方式. 装饰器 一个普通的装 ...
随机推荐
- codeforces Beautiful Numbers
来源:http://codeforces.com/problemset/problem/1265/B B. Beautiful Numbers You are given a permutat ...
- Rsync+inotify数据同步
安装环境 备份服务器端:CentOS7,IP:192.168.1.100 备份客户端:CentOS7,IP:192.168.1.200 服务器端Rsync服务部署 1.安装程序包 # yum –y i ...
- IE浏览器清浮动
.cfx:after,.cfx:before{content:" ";display:table;}.cfx:after{clear:both;}.cfx{*zoom:1;} 今天 ...
- python linux windows 历史版本下载
Index of /ftp/python/ ../ 2.0/ 14-Feb-2019 14:53 - 2.0.1/ 06-Aug-2001 02:14 - 2.1/ 06-Aug-2001 02:14 ...
- CAS 和 ABA 问题
CAS简介 CAS 全称是 compare and swap,是一种用于在多线程环境下实现同步功能的机制. CAS 它是一条CPU并发原语.操作包含三个操作数 -- 内存位置.预期数值和新值.CAS ...
- 3_6 环状序列(UVa1584)
长度为n的环状串有n种表示法,分别为某个位置开始顺时针得到.例如,图中的环状串有10种表示: CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等.在这些表示法中,字典序最小的称为“最小 ...
- 寒假pta二
整除光棍 这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可以被13整除. 现在 ...
- JS利用HTML5的Web Worker实现多线程
需求:有一个长时间的斐波拉契的计算希望放在分线程中计算,计算的得到结果后再返回给主线程展示,再计算的时候不冻结页面 var number = 55;//传入分线程的参数 var worker = ne ...
- Codeforces Round #611 (Div. 3) D
There are nn Christmas trees on an infinite number line. The ii -th tree grows at the position xixi ...
- NOIP2016换教室 BZOJ 4720
BZOJ 4720 换教室 题目描述: 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上 ...