【发布时间】:2017-11-12 00:24:06
【问题描述】:
这是 Python 2.5,也是 GAE,没关系。
我有以下代码。我正在装饰 bar 中的 foo() 方法,使用 dec_check 类作为装饰器。
class dec_check(object):
def __init__(self, f):
self.func = f
def __call__(self):
print 'In dec_check.__init__()'
self.func()
class bar(object):
@dec_check
def foo(self):
print 'In bar.foo()'
b = bar()
b.foo()
执行此操作时,我希望看到:
In dec_check.__init__()
In bar.foo()
但我将TypeError: foo() takes exactly 1 argument (0 given) 设为.foo(),作为对象方法,将self 作为参数。我猜问题是当我执行装饰器代码时bar 的实例实际上并不存在。
那么如何将bar 的实例传递给装饰器类?
【问题讨论】: