装饰器本质上也是函数,接收函数对象来作为参数,并在装饰器的内部来调用接受的函数对象完成相关的函数调用,也可以这样理解   ,为了方便在几个不同函数调用之前或者完成相关的统一操作,注意是完成统一的操作,可以传参数使得装饰器不完全一样,后面会讲到,最重要的应用如工程应用上记录相关的内部接口的流水日志,不同的接口需要统一的样式,所以可以用装饰器来实现,先简单看一下实例:

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 中的装饰器的更多相关文章

  1. 理解Python中的装饰器

    文章先由stackoverflow上面的一个问题引起吧,如果使用如下的代码: @makebold @makeitalic def say(): return "Hello" 打印出 ...

  2. 理解Python中的装饰器//这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档

    转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档 ...

  3. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

  4. 【Python】python中的装饰器——@

    对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...

  5. Python 中实现装饰器时使用 @functools.wraps 的理由

    Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过  ...

  6. 写python中的装饰器

    python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...

  7. python中的装饰器decorator

    python中的装饰器 装饰器是为了解决以下描述的问题而产生的方法 我们在已有的函数代码的基础上,想要动态的为这个函数增加功能而又不改变原函数的代码 例如有三个函数: def f1(x): retur ...

  8. python中@property装饰器的使用

    目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...

  9. 【Python】解析Python中的装饰器

    python中的函数也是对象,函数可以被当作变量传递. 装饰器在python中功能非常强大,装饰器允许对原有函数行为进行扩展,而不用硬编码的方式,它提供了一种面向切面的访问方式. 装饰器 一个普通的装 ...

随机推荐

  1. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  2. about Base64

    用webservice传送文件的时候发现,如果发送的文件中有0x00字符,会被认为是字符串结尾,后面的内容就发送不过去,因此需要对不是纯文本格式的文件做BASE64编码,这样文件中就不会有0x00这样 ...

  3. centos610无桌面安装libreoffice

    Centos610系列配置 #安装文件 yum -y install libreoffice #安装中文包 yum -y install libreoffice-langpack-zh-Han* #安 ...

  4. Ora 命令行建立账号

    .使用cmd 进入 sqlplus /nolog conn system/ as sysdba .修改密码 alter user ebthis identified by new_psw; .查看用户 ...

  5. echart中饼状图的高亮显示。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. vscode vue js 开发插件配置

    安装 vetur { // 自动补全触发范围---双引号内的字符串也可以触发补全 "editor.quickSuggestions": { "other": t ...

  7. java中静态初始化块的执行顺序

    在java中,其应该是先于所有的方法执行. 下面是测试代码: public class Test1 { static{ System.out.println("执行静态初始化块test1.. ...

  8. 在tomcat启动时解析xml文件,获取特定标签的属性值,并将属性值设置到静态变量里

    这里以解析hibernate.cfg.xml数据库配置信息为例,运用dom4j的解析方式来解析xml文件. 1.在javaWeb工程里新建一个java类,命名为GetXmlValue.java,为xm ...

  9. org.springframework.data.redis.RedisConnectionFailureException

    org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested ...

  10. Binary Heap(二叉堆) - 堆排序

    这篇的主题主要是Heapsort(堆排序),下一篇ADT数据结构随笔再谈谈 - 优先队列(堆). 首先,我们先来了解一点与堆相关的东西.堆可以实现优先队列(Priority Queue),看到队列,我 ...