我是 Python 装饰器的新手(哇,很棒的功能!),我很难让以下内容工作,因为self
参数有点混乱。
#this is the decorator
class cacher(object):
def __init__(self, f):
self.f = f
self.cache = {}
def __call__(self, *args):
fname = self.f.__name__
if (fname not in self.cache):
self.cache[fname] = self.f(self,*args)
else:
print "using cache"
return self.cache[fname]
class Session(p.Session):
def __init__(self, user, passw):
self.pl = p.Session(user, passw)
@cacher
def get_something(self):
print "get_something called with self = %s "% self
return self.pl.get_something()
s = Session(u,p)
s.get_something()
当我运行它时,我得到:
get_something called with self = <__main__.cacher object at 0x020870F0>
Traceback:
...
AttributeError: 'cacher' object has no attribute 'pl'
对于我做的那条线self.cache[fname] = self.f(self,*args)
问题- 显然,问题在于self
缓存器对象而不是 Session 实例,它确实没有pl
属性。但是我找不到如何解决这个问题。
我考虑过但不能使用的解决方案——我想让装饰器类返回一个函数而不是一个值(如本文的第 2.1 节),以便self
在正确的上下文中进行评估,但这是不可能的因为我的装饰器被实现为一个类并使用内置 __call__
方法。然后我想不要为我的装饰器使用一个类,这样我就不需要 __call__ 方法,但我不能这样做,因为我需要在装饰器调用之间保持状态(即用于跟踪self.cache
属性中的内容) .
问题- 那么,除了使用全局cache
字典变量(我没有尝试,但假设会起作用)之外,还有其他方法可以使这个装饰器工作吗?
编辑:这个 SO 问题似乎类似于Decorating python 类方法,我如何将实例传递给装饰器?