装饰器详解

Posted qiaoqianshitou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰器详解相关的知识,希望对你有一定的参考价值。

装饰器的原理

装饰器原理就是利用闭包函数来实现,而闭包函数就是内层函数的return和外层函数的作用域名字的引用。

装饰器之所以能这么方便的应用,就是因为python的“一切皆对象”。

为什么用装饰器

能让我们在开发过程中的这些事变的容易:

  1. 分析、日志和手段
  2. 验证及运行检查
  3. 创建框架
  4. 复用不能复用的代码

装饰器的基本用法

@符号是装饰器的语法糖,在定义函数的时候使用,可以避免再次的赋值操作。

@outter等同于foo=outter(foo)

带参装饰器

def use_logging(level):

    def decorator(func):

        def wrapper(*args, **kwargs):

            if level == "warn":

                logging.warn("%s is running" % func.__name__)

            return func(*args)

        return wrapper

 

    return decorator

 

@use_logging(level="warn")

def foo(name=‘foo‘):

    print("i am %s" % name)

 

foo()

 

多个装饰器的执行顺序

def decorator_a(func):

    print ‘Get in decorator_a‘

    def inner_a(*args, **kwargs):

        print ‘Get in inner_a‘

        return func(*args, **kwargs)

    return inner_a

 

def decorator_b(func):

    print ‘Get in decorator_b‘

    def inner_b(*args, **kwargs):

        print ‘Get in inner_b‘

        return func(*args, **kwargs)

    return inner_b

 

@decorator_b

@decorator_a

def f(x):

    print ‘Get in f‘

    return x * 2

 

f(1)

两个函数decotator_a,decotator_b的功能是接收函数作为参数f,然后返回给另一个函数他们各自的inner,在创建的函数inner里调用接收的函数f。

执行结果为:

Get in decorator_a

Get in decorator_b

Get in inner_b

Get in inner_a

Get in f

函数和函数调用:函数在python中也是一个对象,因此函数f是一个对象,其值是函数本身。函数调用f(1)是对函数f传入的参数进行求值得结果。因此,decorator_a函数的返回值是inner_a,inner_a函数在decorator_a里定义。在inner_a里调用f,将f的调用结果作为返回值返回。

用加上一些测试,再debug执行一下,看执行顺序。

def decorator_a(func):

    print(‘Get in decorator_a‘)

    print(func)#拿到的是<function f at 0x02AB45D0>

    def inner_a(*args, **kwargs):

        print(‘Get in inner_a‘)

        return func(*args, **kwargs)

    return inner_a

 

def decorator_b(func):

    print(‘Get in decorator_b‘)

    print(func)#拿到的是<function decorator_a.<locals>.inner_a at 0x02AB44B0>

    def inner_b(*args, **kwargs):

        print(‘Get in inner_b‘)

        return func(*args, **kwargs)

    return inner_b

 

@decorator_b#等于f=decorator_b(f)

@decorator_a

def f(x):

    print(‘Get in f‘)

    return x * 2

 

f(1)

结果:

Get in decorator_a

<function f at 0x02AB45D0>

Get in decorator_b

<function decorator_a.<locals>.inner_a at 0x02AB44B0>

Get in inner_b

Get in inner_a

Get in f

装饰类的装饰器

class Foo(object):

    def __init__(self, func):

      self._func = func

 

    def __call__(self):

      print (‘class decorator runing‘)

      self._func()

      print (‘class decorator ending‘)

 

@Foo

def bar():

    print (‘bar‘)

 

bar()

以上是关于装饰器详解的主要内容,如果未能解决你的问题,请参考以下文章

Python 3 之 装饰器详解

Python装饰器详解

装饰器详解

进阶Python:装饰器 全面详解

Python装饰器详解

Python装饰器详解