BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #4911同步于 2015/1/18
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

[问题]【求助】装饰器实现 报错TypeError

oceansea1911
2015/1/18镜像同步6 回复
import functools def log(text=None): def decorator(func): @functools.wraps(func) def wrapper(* argss,** kw): if args==None: print 'call %s()' %(func.__name__) else : print '%s %s()' %(text,func.__name__) return func(* argss,** kw) return wrapper return decorator @log #@log('hahaha') def f(): print '===============' f() ================================================ 报错: 这个是廖雪峰python教程上的一个例题 实现@log('hahaha')时正常的,但是在实现@log时,报错了,求助各位大神[ema1]
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
asif12机器人#1 · 2015/1/18
@log def f(): print '===============' 等价于f=log(f) @log('hahaha') def f(): print '===============' 等价于f=log('hahaha')(f)
oceansea1911机器人#2 · 2015/1/19
【 在 asif12 的大作中提到: 】 : @log : def f(): : print '===============' : ................... 实际上是把f()作为参数传给了log而没有传给内层的decorator,所以会报出decorato没有参数的异常,是这样理解吗?
asif12机器人#3 · 2015/1/19
【 在 oceansea1911 的大作中提到: 】 : : 实际上是把f()作为参数传给了log而没有传给内层的decorator,所以会报出decorato没有参数的异常,是这样理解吗? 是
jh1机器人#4 · 2015/1/19
妈蛋,汪汪。。
oceansea1911机器人#5 · 2015/1/19
【 在 asif12 的大作中提到: 】 : : 是 [ema20][ema20][ema20]
oceansea1911机器人#6 · 2015/1/19
【 在 jh1 的大作中提到: 】 : 妈蛋,汪汪。。 亮亮现身了~