decorator接受一个函数,然后添加一些功能,最后返回这个函数。在这篇文章中,你会知道如何创建decorator以及为什么要用这个语法。
1. What are decorators in Python?
python有个有意思的特性叫decorator,在现存代码中添加某些功能。这叫做meta-programming,试图在编译时修改另一部分程序。
2. Prerequisites for learning decorators
为了理解decorator,我们首先弄清楚Python中的一些基础。我们一定要接受Python的观点,一切皆对象。意味着我们只要定义与这些对象相关的identifier就行。其中函数也不例外,他们也是对象。不同的名字可以绑定相同的函数对象。
def first(msg):
print(msg)
first("Hello")
second = first
second("Hello")
你在跑代码的时候,first 和second给出了相同的输出,在这,first和second都引用相同的函数对象。
下面看看更奇怪的事情。
函数作为参数传递到另外一个函数中。
如果你用过诸如map,filter,reduce这样的函数,你就这道什么意思了。
把函数作为参数的函数我们叫higher order functions。下面给出个例子:
def inc(x):
return x + 1
def dec(x):
return x - 1
def operate(func, x):
result = func(x)
return result
#运行结果如下:
>>> operate(inc,3)
4
>>> operate(dec,3)
2
而且,函数也能返回另一个函数
def is_called():
def is_returned():
print("Hello")
return is_returned
new = is_called()
#Outputs "Hello"
new()
3. Getting back to Decorators
当函数能被调用时,我们称之为callable。
实际上,任何实现特殊方法的 call() 的对象都叫callable,所以在绝大多数的意思当中,a decorator is a callable that returns a callable
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
#运行结果:
>>> ordinary()
I am ordinary
>>> # let's decorate this ordinary function
>>> pretty = make_pretty(ordinary)
>>> pretty()
I got decorated
I am ordinary
在上面的例子中,==make_pretty()==就是个decorator。
pretty = make_pretty(ordinary)
函数ordinary()被装饰了,返回来的函数被给了个名字pretty。
我们看到:decorator函数给原始函数添加了新功能。这就像打包一个礼物,decorator就像wrapper,使得礼物更好看,增加了功能。
通常情况下,我们装饰一个函数并且重新赋值如下:
ordinary = make_pretty(ordinary)
这是一个通用结构,所以Python专门有个简化使用它的语法。
我们在decorator函数的名字前使用@符号,把它放在要被装饰的函数定义上:
@make_pretty
def ordinary():
print("I am ordinary")
等同于
def ordinary():
print("I am ordinary")
ordinary = make_pretty(ordinary)
这就是实现decorators的语法糖(syntactic sugar)
4. Decorating Functions with Parameters
上面的decorator很简单,其函数都没有参数。要是函数带参数怎么样呢?
def divide(a, b):
return a/b
这个函数有两个参数, a a a和 b b b。我们知道如果 b b b是0,会引起错误。
>>> divide(2,5)
0.4
>>> divide(2,0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
现在我们造一个decorator来修补引起错误的情况。
def smart_divide(func):
def inner(a,b):
print("I am going to divide",a,"and",b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a,b)#在这返回的是传入的函数参数
return inner#返回nested function
@smart_divide
def divide(a,b):
return a/b
新的做法如果触发错误条件则会返回None。眼尖的人会发现decorator内部的nested函数的参数和它要装饰的函数的参数一模一样。下面我们给出通用的带有任何参数数量的decorator形式。
在Python中,这种形式一般写成function(*args, **kwargs)。其中,args是position argument的tuple。kwargs是keyword argument的字典。
def works_for_all(func):
def inner(*args, **kwargs):
print("I can decorate any function")
return func(*args, **kwargs)
return inner
5. Chaining Decorators in Python
多decorator在Python可以链式叠加。
也就是说,函数可以被不同或者相同的decorator装饰多次。而且只需要把这些装饰器放在要装饰的函数上即可。
def star(func):
def inner(*args, **kwargs):
print("*" * 30)
func(*args, **kwargs)
print("*" * 30)
return inner
def percent(func):
def inner(*args, **kwargs):
print("%" * 30)
func(*args, **kwargs)
print("%" * 30)
return inner
@star
@percent
def printer(msg):
print(msg)
printer("Hello")
输出:
******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************
上面的语法
@star
@percent
def printer(msg):
print(msg)
等价于
def printer(msg):
print(msg)
printer = star(percent(printer))
链式叠加的顺序很重要。如果把上面的顺序倒过来
@percent
@star
def printer(msg):
print(msg)
输出结果:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************
Hello
******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%