加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何将自己传递给装饰者?

发布时间:2020-12-16 23:46:50 所属栏目:Python 来源:网络整理
导读:如何将self.key传递给装饰器? class CacheMix(object): def __init__(self,*args,**kwargs): super(CacheMix,self).__init__(*args,**kwargs) key_func = Constructor( memoize_for_request=True,params={'updated_at': self.key} ) @cache_response(key_fu

如何将self.key传递给装饰器?

    class CacheMix(object):

        def __init__(self,*args,**kwargs):
            super(CacheMix,self).__init__(*args,**kwargs)

        key_func = Constructor(
            memoize_for_request=True,params={'updated_at': self.key}
        )

        @cache_response(key_func=key_func)
        def list(self,**kwargs):
            pass

    class ListView(CacheMix,generics.ListCreateAPIView):
        key = 'test_key'

我收到错误:

'self' is not defined
最佳答案
这是一个用类装饰器做的例子,我试着在评论中向你描述.我在你的问题中填写了一些未定义的引用,并使用了cache_response函数装饰器的超简化版本,但希望这将足够具体地传达这个想法,使你能够适应你的真实代码.

import inspect
import types

class Constructor(object):
    def __init__(self,memoize_for_request=True,params=None):
        self.memoize_for_request = memoize_for_request
        self.params = params
    def __call__(self):
        def key_func():
            print('key_func called with params:')
            for k,v in self.params.items():
                print('  {}: {!r}'.format(k,v))
        key_func()

def cache_response(key_func):
    def decorator(fn):
        def decorated(*args,**kwargs):
            key_func()
            fn(*args,**kwargs)
        return decorated
    return decorator

def example_class_decorator(cls):
    key_func = Constructor(  # define key_func here using cls.key
        memoize_for_request=True,params={'updated_at': cls.key} # use decorated class's attribute
    )
    # create and apply cache_response decorator to marked methods
    # (in Python 3 use types.FunctionType instead of types.UnboundMethodType)
    decorator = cache_response(key_func)
    for name,fn in inspect.getmembers(cls):
        if isinstance(fn,types.UnboundMethodType) and hasattr(fn,'marked'):
            setattr(cls,name,decorator(fn))
    return cls

def decorate_me(fn):
    setattr(fn,'marked',1)
    return fn

class CacheMix(object):
    def __init__(self,**kwargs):
        super(CacheMix,**kwargs)

    @decorate_me
    def list(self,**kwargs):
        classname = self.__class__.__name__
        print('list() method of {} object called'.format(classname))

@example_class_decorator
class ListView(CacheMix):
    key = 'test_key'

listview = ListView()
listview.list()

输出:

key_func called with params:
  updated_at: 'test_key'
list() method of ListView object called

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读