• 【Python学习】装饰器


    装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象作为某一个函数的返回结果。相对于其它方式,装饰器语法简单,代码可读性高。因此,装饰器在Python项目中有广泛的应用。

    装饰器最早在Python 2.5中出现,它最初被用于加工函数和方法这样的可调用对象(callable object,这样的对象定义有__call__方法)。在Python 2.6以及之后的Python版本中,装饰器被进一步用于加工类。

    装饰函数和方法

    我们先定义两个简单的数学函数,一个用来计算平方和,一个用来计算平方差:

    复制代码
    # get square sum
    def square_sum(a, b):
        return a**2 + b**2
    
    # get square diff
    def square_diff(a, b):
        return a**2 - b**2

    print(square_sum(3, 4))
    print(square_diff(3, 4))
    复制代码

    在拥有了基本的数学功能之后,我们可能想为函数增加其它的功能,比如打印输入。我们可以改写函数来实现这一点:

    复制代码
    # modify: print input
    
    # get square sum
    def square_sum(a, b):
        print("intput:", a, b)
        return a**2 + b**2
    
    # get square diff
    def square_diff(a, b):
        print("input", a, b)
        return a**2 - b**2

    print(square_sum(3, 4))
    print(square_diff(3, 4))
    复制代码

    我们修改了函数的定义,为函数增加了功能。

    现在,我们使用装饰器来实现上述修改:

    复制代码
    def decorator(F):
        def new_F(a, b):
            print("input", a, b)
            return F(a, b)
        return new_F
    
    # get square sum
    @decorator
    def square_sum(a, b):
        return a**2 + b**2
    
    # get square diff
    @decorator
    def square_diff(a, b):
        return a**2 - b**2
    
    print(square_sum(3, 4))
    print(square_diff(3, 4))
    复制代码

    装饰器可以用def的形式定义,如上面代码中的decorator。装饰器接收一个可调用对象作为输入参数,并返回一个新的可调用对象。装饰器新建了一个可调用对象,也就是上面的new_F。new_F中,我们增加了打印的功能,并通过调用F(a, b)来实现原有函数的功能。

    定义好装饰器后,我们就可以通过@语法使用了。在函数square_sum和square_diff定义之前调用@decorator,我们实际上将square_sum或square_diff传递给decorator,并将decorator返回的新的可调用对象赋给原来的函数名(square_sum或square_diff)。 所以,当我们调用square_sum(3, 4)的时候,就相当于:

    square_sum = decorator(square_sum)
    square_sum(3, 4)

    我们知道,Python中的变量名和对象是分离的。变量名可以指向任意一个对象。从本质上,装饰器起到的就是这样一个重新指向变量名的作用(name binding),让同一个变量名指向一个新返回的可调用对象,从而达到修改可调用对象的目的。

    与加工函数类似,我们可以使用装饰器加工类的方法。

    如果我们有其他的类似函数,我们可以继续调用decorator来修饰函数,而不用重复修改函数或者增加新的封装。这样,我们就提高了程序的可重复利用性,并增加了程序的可读性。

    含参的装饰器

    在上面的装饰器调用中,比如@decorator,该装饰器默认它后面的函数是唯一的参数。装饰器的语法允许我们调用decorator时,提供其它参数,比如@decorator(a)。这样,就为装饰器的编写和使用提供了更大的灵活性。

    复制代码
    # a new wrapper layer
    def pre_str(pre=''):
        # old decorator
        def decorator(F):
            def new_F(a, b):
                print(pre + "input", a, b)
                return F(a, b)
            return new_F
        return decorator
    
    # get square sum
    @pre_str('^_^')
    def square_sum(a, b):
        return a**2 + b**2
    
    # get square diff
    @pre_str('T_T')
    def square_diff(a, b):
        return a**2 - b**2
    
    print(square_sum(3, 4))
    print(square_diff(3, 4))
    复制代码

    上面的pre_str是允许参数的装饰器。它实际上是对原有装饰器的一个函数封装,并返回一个装饰器。我们可以将它理解为一个含有环境参量的闭包。当我们使用@pre_str('^_^')调用的时候,Python能够发现这一层的封装,并把参数传递到装饰器的环境中。该调用相当于:

    square_sum = pre_str('^_^') (square_sum)

    装饰类

    在上面的例子中,装饰器接收一个函数,并返回一个函数,从而起到加工函数的效果。在Python 2.6以后,装饰器被拓展到类。一个装饰器可以接收一个类,并返回一个类,从而起到加工类的效果。

    复制代码
    def decorator(aClass):
        class newClass:
            def __init__(self, age):
                self.total_display   = 0
                self.wrapped         = aClass(age)
            def display(self):
                self.total_display += 1
                print("total display", self.total_display)
                self.wrapped.display()
        return newClass
    
    @decorator
    class Bird:
        def __init__(self, age):
            self.age = age
        def display(self):
            print("My age is",self.age)
    
    eagleLord = Bird(5)
    for i in range(3):
        eagleLord.display()
    复制代码

    在decorator中,我们返回了一个新类newClass。在新类中,我们记录了原来类生成的对象(self.wrapped),并附加了新的属性total_display,用于记录调用display的次数。我们也同时更改了display方法。

    通过修改,我们的Bird类可以显示调用display的次数了。

    总结

    装饰器的核心作用是name binding。这种语法是Python多编程范式的又一个体现。大部分Python用户都不怎么需要定义装饰器,但有可能会使用装饰器。鉴于装饰器在Python项目中的广泛使用,了解这一语法是非常有益的。

     1 # !/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 from functools import wraps #用于解决装饰器影响被装饰函数__name__,__doc__等内置函数的返回值
     4 
     5 
     6 def log1(level):
     7     """
     8     第一种方式
     9     :param level:
    10     :return:
    11     """
    12     def debug(func):
    13         @wraps(func)
    14         def wrapper(*args, **kwargs):
    15             print "[{}]: enter {}()".format(level, func.__name__)
    16             if level == "level1":
    17                 print "curent is %s" % level
    18                 return func(*args, **kwargs)
    19             else:
    20                 print "not exec func"
    21         return wrapper
    22     return debug
    23 
    24 
    25 class Log2:
    26     """
    27     第二种方式
    28     """
    29     def __init__(self, level):
    30         self.level = level
    31 
    32     def __call__(self, func):
    33         @wraps(func)
    34         def wrapper(*args, **kwargs):
    35             print "[{}]: enter {}()".format(self.level, func.__name__)
    36             if self.level == "level1":
    37                 print "curent is %s" % self.level
    38                 return func(*args, **kwargs)
    39             else:
    40                 print "not exec func"
    41         return wrapper
    42 
    43 
    44 @Log2("level1")
    45 def say_hello(*args, **kwargs):
    46     print("args=%s" % args)
    47     for k, v in kwargs.items():
    48         print("k=%s,v=%s" % (k, v))
    49     print "hello!"
    50 
    51 
    52 say_hello((1,2,3), a=1)
    53 type(say_hello)
    54 print say_hello.__name__
    55 
    56 import inspect
    57 print inspect.getargspec(say_hello)  # failed
    58 #print inspect.getsource(say_hello)  # failed
    59 
    60 from decorator import decorator
     1 class Counter:
     2     def __init__(self, func):
     3         self.func = func
     4         self.count = 0
     5 
     6     def __call__(self, *args, **kwargs):
     7         self.count += 1
     8         return self.func(*args, **kwargs)
     9 
    10 @Counter
    11 def foo():
    12     pass
    13 
    14 for i in range(10):
    15     foo()
    16 
    17 print(foo.count)  # 10
    作者:gtea 博客地址:https://www.cnblogs.com/gtea
  • 相关阅读:
    [转]spring学习笔记7.自动装配(补充)
    [转]JSON 入门指南
    [转]spring学习笔记补: 生命周期
    rownum的用法
    简易计算器实现
    Flush the AOS cache from code
    通过Batch发送Report
    Export excel file format
    The report's image auto resizes (Always use in report)
    Save the users last values in a dialog
  • 原文地址:https://www.cnblogs.com/gtea/p/12678830.html
Copyright © 2020-2023  润新知