【发布时间】:2012-12-25 22:58:07
【问题描述】:
我正在通过How to make a chain of function decorators? 了解装饰器。
在下面的示例中,我们看到“method_to_decorate”由于闭包而可以被包装函数访问。
但是,我不明白包装函数如何访问参数 self 和 lie。
def method_friendly_decorator(method_to_decorate):
def wrapper(self, lie):
lie = lie - 3 # very friendly, decrease age even more :-)
return method_to_decorate(self, lie)
return wrapper
class Lucy(object):
def __init__(self):
self.age = 32
@method_friendly_decorator
def sayYourAge(self, lie):
print "I am %s, what did you think?" % (self.age + lie)
l = Lucy()
l.sayYourAge(-3)
#outputs: I am 26, what did you think?
【问题讨论】:
标签: python closures decorator python-decorators