获取装饰器内的方法类

时间:2014-06-20 19:21:50

标签: python decorator python-decorators

我有这段代码:

class Application(object):
    def cache(self, obj_to_cache):
        def decorator(f):
            print 'apply decorator'
            return f
        return decorator

app = Application()

class A(object):
    cache = []

    def add_cache(self, obj):
        self.cache.append(obj)

    def print_cache(self):
        print 'cache: ', self.cache

class B(A):
    @app.cache('Hello')
    def get(self):
        print 'get B'

    @app.cache('World')
    def another(self):
        print 'another B'

b = B()
b.print_cache()

获取结果:

apply decorator
apply decorator
cache:  []

我的问题是:如何从方法def decorator(f):访问对象A并致电add_cache

我想要的结果是:

apply decorator
apply decorator
cache:  ['Hello', 'Word']

0 个答案:

没有答案