python三层装饰器-python 三层嵌套定义装饰器

def deco(arg): #最外层函数定义的参数是=装饰函数的参数,该参数也可以是个类

def _deco(func): #中间层函数定义的参数是=被装饰函数的名称

def __deco(*args,**kwargs): #最内层函数定义的参数=传递给被装饰函数

print '='*40

print "before %s called [%s]." % (func.__name__,arg)

ret=func(*args,**kwargs)

print func #打印被装饰函数的内存地址

print "after %s called.[%s]." % (func.__name__,arg)

return ret #返回被装饰函数的返回值

return __deco #将最内层函数作为值返回

return _deco #返回中间层函数

@deco("mymodule")

def myfunc(a,b):

print ("myfunc(%s,%s) called" % (a,b))

return a + b

@deco("mymodule2")

def myfunc2(a,b,c):

print ("myfunc(%s,%s,%s) called." % (a,b,c))

return a + b + c

myfunc(1,4)

myfunc2(5,6,9)

阅读(1498) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值