装饰器 港控/mmm° 2022-05-28 13:27 251阅读 0赞 以前你有没有这样一段经历:很久之前你写过一个函数,现在你突然有了个想法就是你想看看,以前那个函数在你数据集上的运行时间是多少,这时候你可以修改之前代码为它加上计时的功能,但是这样的话是不是还要大体读读你之前的这个的代码,稍微搞清楚一点它的逻辑,才敢给它添加新的东西。这样是不是很繁琐,要是你之前写的代码足够乱足够长,再去读它是不是很抓狂...。实际工作中,我们常常会遇到这样的场景,可能你的需求还不只是这么简单。那么有没有一种可以不对源码做任何修改,并且可以很好的实现你所有需求的手段呢?答案当然是有,这就是今天我们要介绍的python装饰器。有了装饰器,你除了不用担心前面提到的问题,并且还可以很好的处理接下来要做的事:那就是现在你又有了一个新的需求,比如为另一个函数添加计时功能,这时就非常简单了,把要装饰的函数丢给装饰器就好了,它会自动给你添加完功能并返回给你。是不是很神奇?下面我们将一层层剥开它的神秘面纱。 ## 1. 闭包函数 ## 在看装饰器之前,我们先来搞清楚什么是闭包函数。python是一种面向对象的编程语言,在python中一切皆对象,这样就使得变量所拥有的属性,函数也同样拥有。这样我们就可以理解在函数内创建一个函数的行为是完全合法的。这种函数被叫做内嵌函数,这种函数只可以在外部函数的作用域内被正常调用,在外部函数的作用域之外调用会报错,例如: ![1251096-20171028145152195-1360676939.png][] 而如果内部函数里引用了外部函数里定义的对象(甚至是外层之外,但不是全局变量),那么此时内部函数就被称为闭包函数。闭包函数所引用的外部定义的变量被叫做自由变量。闭包从语法上看非常简单,但是却有强大的作用。闭包可以将其自己的代码和作用域以及外部函数的作用结合在一起。下面给出一个简单的闭包的例子: ![复制代码][copycode.gif] def count(): a = 1 b = 1 def sum(): c = 1 return a + c # a - 自由变量 return sum ![复制代码][copycode.gif] 总结:什么函数可以被称为闭包函数呢?主要是满足两点:函数内部定义的函数;引用了外部变量但非全局变量。 ## 2. python装饰器 ## 有了闭包函数的概念,我们再去理解装饰器会相对容易一些。python装饰器本质上就是一个函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外的功能,装饰器的返回值也是一个函数对象(函数的指针)。装饰器函数的外部函数传入我要装饰的函数名字,返回经过修饰后函数的名字;内层函数(闭包)负责修饰被修饰函数。从上面这段描述中我们需要记住装饰器的几点属性,以便后面能更好的理解: 实质: 是一个函数 参数:是你要装饰的函数名(并非函数调用) 返回:是装饰完的函数名(也非函数调用) 作用:为已经存在的对象添加额外的功能 特点:不需要对对象做任何的代码上的变动 python装饰器有很多经典的应用场景,比如:插入日志、性能测试、事务处理、权限校验等。装饰器是解决这类问题的绝佳设计。并且从引入中的列子中我们也可以归纳出:装饰器最大的作用就是对于我们已经写好的程序,我们可以抽离出一些雷同的代码组建多个特定功能的装饰器,这样我们就可以针对不同的需求去使用特定的装饰器,这时因为源码去除了大量泛化的内容而使得源码具有更加清晰的逻辑。 ### 2.1 函数装饰器 ### #### 函数的函数装饰器 #### 我们还是以为函数添加计时功能为例,讲述函数装饰器。 ![复制代码][copycode.gif] import time def decorator(func): def wrapper(*args, **kwargs): start_time = time.time() func() end_time = time.time() print(end_time - start_time) return wrapper @decorator def func(): time.sleep(0.8) func() # 函数调用 # 输出:0.800644397735595 ![复制代码][copycode.gif] 在上面代码中 func是我要装饰器的函数,我想用装饰器显示func函数运行的时间。@decorator这个语法相当于 执行 func = decorator(func),为func函数装饰并返回。在来看一下我们的装饰器函数 - decorator,该函数的传入参数是func (被装饰函数),返回参数是内层函数。这里的内层函数-wrapper,其实就相当于闭包函数,它起到装饰给定函数的作用,wrapper参数为\*args, \*\*kwargs。\*args表示的参数以列表的形式传入;\*\*kwargs表示的参数以字典的形式传入: ![1251096-20171028155445320-839180786.png][] 从图中我们可以看到:凡是以key=value形式的参数均存在kwargs中,剩下的所有参数都以列表的形式存于args中。这里要注意的是:为了不破坏原函数的逻辑,我们要保证内层函数wrapper和被装饰函数func的传入参数和返回值类型必须保持一致。 #### 类方法的函数装饰器 #### 类方法的函数装饰器和函数的函数装饰器类似。 ![复制代码][copycode.gif] import time def decorator(func): def wrapper(me_instance): start_time = time.time() func(me_instance) end_time = time.time() print(end_time - start_time) return wrapper class Method(object): @decorator def func(self): time.sleep(0.8) p1 = Method() p1.func() # 函数调用 ![复制代码][copycode.gif] 对于类方法来说,都会有一个默认的参数self,它实际表示的是类的一个实例,所以在装饰器的内部函数wrapper也要传入一个参数 -me\_instance就表示将类的实例p1传给wrapper,其他的用法都和函数装饰器相同。 ### 2.2 类装饰器 ### 前面我们提到的都是让 函数作为装饰器去装饰其他的函数或者方法,那么可不可以让 一个类发挥装饰器的作用呢?答案肯定是可以的,一切皆对象嚒,函数和类本质没有什么不一样。类的装饰器是什么样子的呢? ![复制代码][copycode.gif] class Decorator(object): def __init__(self, f): self.f = f def __call__(self): print("decorator start") self.f() print("decorator end") @Decorator def func(): print("func") func() ![复制代码][copycode.gif] 这里有注意的是:\_\_call\_\_()是一个特殊方法,它可将一个类实例变成一个可调用对象: p = Decorator(func) # p是类Decorator的一个实例 p() # 实现了__call__()方法后,p可以被调用 要使用类装饰器必须实现类中的\_\_call\_\_()方法,就相当于将实例变成了一个方法。 ### 2.3 装饰器链 ### 一个python函数也可以被多个装饰器修饰,要是有多个装饰器时,这些装饰器的执行顺序是怎么样的呢? ![1251096-20171028170526992-2025082647.png][] 可见,多个装饰器的执行顺序:是从近到远依次执行。 ### 2.4 python装饰器库 - functools ### ![复制代码][copycode.gif] def decorator(func): def inner_function(): pass return inner_function @decorator def func(): pass print(func.__name__) # 输出: inner_function ![复制代码][copycode.gif] 上述代码最后执行的结果不是 func,而是 inner\_function!这表示被装饰函数自身的信息丢失了!怎么才能避免这种问题的发生呢? 可以借助functools.wraps()函数: ![复制代码][copycode.gif] from functools import wraps def decorator(func): @wraps(func) def inner_function(): pass return inner_function @decorator def func(): pass print(func.__name__) #输出: func ![复制代码][copycode.gif] ### 3、问答时间 ### 问题:被装饰的函数如果有参数呢? <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">12</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"><span style="font-family:SimSun;font-size:16px;">#一个参数</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">f1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'f1'</span></span> </div> </div></td> </tr> </tbody> </table> <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">12</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"><span style="font-family:SimSun;font-size:16px;">#两个参数</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">f1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'f1'</span></span> </div> </div></td> </tr> </tbody> </table> <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">12</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"><span style="font-family:SimSun;font-size:16px;">#三个参数</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg3</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg3</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">f1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg3</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'f1'</span></span> </div> </div></td> </tr> </tbody> </table> 问题:可以装饰具有处理n个参数的函数的装饰器? <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">f1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg3</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'f1'</span></span> </div> </div></td> </tr> </tbody> </table> 问题:一个函数可以被多个装饰器装饰吗? <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">12</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">13</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">14</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">15</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">16</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">17</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">18</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">19</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">20</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">21</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">inner</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"># 验证3</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">args</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">*</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kwargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">inner</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;">w1</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">w2</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">f1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg1</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg2</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">arg3</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'f1'</span></span> </div> </div></td> </tr> </tbody> </table> 问题:还有什么更吊的装饰器吗? <table style="margin-left:0px;border:none;margin-top:0px;margin-right:0px;margin-bottom:0px;padding:0px;border-spacing:0px;width:auto;background:none;table-layout:auto;"> <tbody style="margin:0px;padding:0px;"> <tr style="background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <td style="text-align:center;background-image:none;border-style:none;margin:0px;padding:0px;vertical-align:top;background-color:rgb(238,238,238);"> <div style="border:none;margin:0px;padding-right:0px;padding-left:0px;background:none;text-align:left;white-space:nowrap;padding-top:5px;padding-bottom:3px;line-height:15px;"> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">1</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">2</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">3</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">4</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">5</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">6</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">7</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">8</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">9</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">10</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">11</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">12</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">13</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">14</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">15</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">16</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">17</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">18</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">19</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">20</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">21</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">22</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">23</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">24</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">25</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">26</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">27</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">28</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">29</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">30</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">31</span> </div> <div style="border-top:none;border-bottom:none;border-left:none;margin:0px;padding:0px 5px;background:none;height:inherit;border-right:1px solid rgb(222,222,222);text-align:right;min-width:1.2em;color:rgb(170,170,170);"> <span style="font-family:SimSun;font-size:16px;">32</span> </div> </div></td> <td style="text-align:center;width:582px;background:none;border-style:none;margin:0px;padding:0px;vertical-align:top;"> <div style="margin:0px;padding-right:0px;padding-left:0px;text-align:left;color:rgb(0,0,0);white-space:pre;border:none;padding-top:5px;padding-bottom:3px;background:none;line-height:15px;"> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"><span style="font-family:SimSun;font-size:16px;">#!/usr/bin/env python</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(184,92,0);"><span style="font-family:SimSun;font-size:16px;">#coding:utf-8</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">Before</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'before'</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">After</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'after'</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">Filter</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">before_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">after_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">outer</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">main_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">wrapper</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">before</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">before_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">if</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">before</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">!=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">None</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">before_result</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">;</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">main</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">main_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">if</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">main</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">!=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">None</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">main_result</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">;</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">after</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">after_func</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">if</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">after</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">_</span>result <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">!=</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">None</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">after_result</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">;</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">wrapper</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;font-weight:bold;">return</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;">outer</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> </span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">@</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">Filter</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">Before</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">After</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"><span style="border:0px;margin:0px;padding:0px;height:inherit;">def</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:#008080;">Index</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">(</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">request</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">,</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,45,122);">kargs</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(51,51,51);">)</span><span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(0,111,224);">:</span></span> </div> <div style="border:none;margin:0px;padding:0px 5px;background:none;height:inherit;"> <span style="font-family:SimSun;font-size:16px;"> <span style="border:0px;margin:0px;padding:0px;height:inherit;">print</span> <span style="border:0px;margin:0px;padding:0px;height:inherit;color:rgb(221,17,68);">'index'</span></span> </div> </div></td> </tr> </tbody> </table> [1251096-20171028145152195-1360676939.png]: /images/20220528/86c3da307c7d48e39b8eb0cd5a753999.png [copycode.gif]: /images/20220528/f0457f310554416fa78598f3bc782134.png [1251096-20171028155445320-839180786.png]: /images/20220528/0463bca0438441e5823fc923faa8e156.png [1251096-20171028170526992-2025082647.png]: /images/20220528/e253c64299024c7793fca1c7cf529028.png
相关 装饰器 装饰器 定义: 用来装饰的工具 在不改变源代码及调用方式的基础上额外增加新的功能 开放封闭原则: 扩展是开放的(增加新功能) 修改源码是封闭的(修改已经实现 柔光的暖阳◎/ 2023年10月10日 10:16/ 0 赞/ 60 阅读
相关 装饰器 [https://www.cnblogs.com/cicaday/p/python-decorator.html][https_www.cnblogs.com_cicaday_ ╰半橙微兮°/ 2023年01月06日 01:24/ 0 赞/ 142 阅读
相关 【python】闭包和装饰器、通用装饰器、多个装饰器、带参装饰器、类装饰器 1、闭包 概念: 把使用了外部函数变量的内部函数称为闭包 构成闭包的三个条件: 在函数嵌套的基础上(在外部函数内定义一个内部函数) 内部函数使用外部函数变 - 日理万妓/ 2022年11月28日 13:44/ 0 赞/ 253 阅读
相关 装饰器 装饰器 装饰模式有很多经典的使用场景,例如插入日志、性能测试、事务处理等等,有了装饰器,就可以提取大量函数中与本身功能无关的类似代码,从而达到代码重用的目的。 装饰器 ╰+攻爆jí腚メ/ 2022年06月07日 11:58/ 0 赞/ 245 阅读
相关 装饰器 以前你有没有这样一段经历:很久之前你写过一个函数,现在你突然有了个想法就是你想看看,以前那个函数在你数据集上的运行时间是多少,这时候你可以修改之前代码为它加上计时的功能,但是这 港控/mmm°/ 2022年05月28日 13:27/ 0 赞/ 252 阅读
相关 装饰器 装饰器 如果我们要增强函数的功能,但又不希望修改`原`函数的定义,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator); 本质上,de 墨蓝/ 2021年12月14日 14:17/ 0 赞/ 263 阅读
相关 装饰器 目录 普通装饰器 带参数的装饰器 多个装饰器装饰一个函数 装饰器的修复技术 装饰器 :在不改变原函数的代号和调用方法的基础上, 给原函数额外增加功能 女爷i/ 2021年10月29日 16:26/ 0 赞/ 377 阅读
相关 装饰器 装饰器的固定模式 def warpper(f): def inner(args,kwargs): print("WDNMD") 我就是我/ 2021年10月29日 07:12/ 0 赞/ 503 阅读
相关 装饰器 定义: 器字代表函数,装饰器本质是函数;装饰:装饰其他函数,就是为其他函数添加附加功能 原则: 1.不能修改被装饰函数的源代码(在不修改被装饰函数源代码的情况下为 心已赠人/ 2021年09月20日 12:00/ 0 赞/ 388 阅读
还没有评论,来说两句吧...