python decorator用法_python小例子之8 -- decorator的使用

文章源自:http://bluecrystal.iteye.com/blog/137462

python小例子之8 -- decorator的使用

主题: decorator的使用

环境: winxp pro + sp2 + python2.5

备注: 请注意,凡是在源代码文件中使用了中文字符,请最好保存为utf-8格式

本文就是因kdekid 在该blog回帖后,我对python decorator的使用整理出的一点心得,在上述提及的blog中的代码亦可修改为本文所描述的方式,各位同学可自己试试 :)

具体的语法描述可参考: python manuals -- Language Reference -- 7.Compound statements -- 7.6 Function definitions

# decorator_arg.py

# 修饰函数

def decorator(fun):

def ifun(*args, **kwargs):

args = (i+1 for i in args)

return fun(*args, **kwargs)

return ifun

def decorator1(arg):

def _decorator1(fun):

def ifun(*args, **kwargs):

args = (i+arg for i in args)

return fun(*args, **kwargs)

return ifun

return _decorator1

# 被修饰函数1

@decorator

def fun1(x,y,z):

return x+y+z

arg = 2

# 被修饰函数2

@decorator1(arg)

def fun2(x,y,z):

return x+y+z

# 测试代码

a = 3

b = 4

c = 5

print fun1(a,b,c)

print fun2(a,b,c)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值