Python中的装饰器本质上是一个高阶函数,它接收一个函数,并且对这个函数进行包装,从而改变原函数的默认行为。
@deco1(deco_arg) @deco2 def func(): pass
相当于
func = deco1(deco_arg)(deco2(func))
Demo如下:
# -*-coding:utf-8 -*- def decorator(str): print str def retfn(fn): def retfn(*arg): print '已装饰' # 装饰 return fn(*arg) # 调用原始函数 return retfn return retfn @decorator("实例化装饰器") def test(str0, str1): print str0, str1 test('Hello', 'world');