关于 decorator
求详解,第一种多的一层包裹到底是用来干嘛的
这种装饰器很常见,也没问过为什么
>>> def log(func):
... def wrapper(*args, **kwargs):
... print "call: ", func.__name__
... return func(*args, **kwargs)
... return wrapper
...
>>> @log
... def now():
... print "It's now..."
...
>>> now()
call: now
It's now...
这种... 我第一次见,求详解
>>> def log(func):
... print "call: ", func.__name__
... return func
...
>>> @log
... def now():
... print "It's now......"
...
call: now
>>> now()
It's now......
灼眼D夏娜酱
10 years, 3 months ago
Answers
装饰器函数的第一层用于接收被装饰函数的函数本身,第二层用于接收调用被装饰函数时,传递的参数。关于这个,我写过一篇blog
http://coolbit.in/blog/2014/03/29/decorator-exception-handling/
香辣盾蟹不加料
answered 10 years, 3 months ago