関数デコレータのチェーンを作成する方法を説明していますか? デコレータを理解する。
次の例では、クロージャが原因で「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?