一:以下两端代码作用一样:
无装饰器:
def action(x): return(x) def action_pro(n): def warpper(x): return(n(x) * x) return(warpper) action = action_pro(action) #第一个action为自定义的伪装变量,第二个action为上边定义的action函数 action(3) #此函数实际为warpper(3),返回值为9
有装饰器:
def action_pro(n): def warpper(x): return(n(x) * x) return(warpper) @action_pro #用action_pro函数把action包装成warpper def action(x): return(x) action(3) #此函数实际为warpper(3),返回值为9
详细参考地址:http://pythonmap.iteye.com/blog/1682696