推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
KIDJourney
V2EX  ›  Python

Python 中使用类修饰器修饰类方法如何处理 self?

  •  
  •   KIDJourney ·
    KIDJourney · Jan 27, 2016 · 4340 views
    This topic created in 3944 days ago, the information mentioned may be changed or developed.

    这个是装饰器类。

    class PostCache:
        def __init__(self, func):
            self.func = func
            self.redis = redis.StrictRedis()
    
        def __call__(self, url_list):
            url_not_cached = []
            for url in url_list:
                if self.redis.get(url):
                    self.redis.expire(url, 600)
                else:
                    url_not_cached.append(url)
                    self.redis.set(url, '1')
                    self.redis.expire(url, 600)
    
            return self.func(url_not_cached)
    

    这个是要被装饰的方法。

    @rediscache.PostCache
        def __get_content_list(self, url_list):
            content_list = []
    
            for url in url_list:
                content_list.append(self.get_content(url))
    
                time.sleep(config_intervaltime())
    
            return content_list
    

    然后报错

    File "crawler.py", line 28, in get_posts
        post_content_list = self.__get_content_list(url_list)
    TypeError: __call__() missing 1 required positional argument: 'url_list'
    

    该如何解决呢?

    9 replies  •  2016-01-28 00:29:04 +08:00
    kinghui
    kinghui
    Jan 27, 2016
    1
    ```python
    @rediscache.PostCache()
    def __get_content_list(self, url_list):
    ....
    ```
    bobuick
    bobuick
    Jan 27, 2016❤️1
    2
    你需要确保它实现了 __call__() 和 __get__() 方法
    kinghui
    kinghui
    Jan 27, 2016
    3
    看错了, 请忽略我
    KIDJourney
    KIDJourney
    OP
    Jan 27, 2016
    4
    @bobuick 为什么要实现__get__()呢?
    [PythonDecoratorLibrary]( https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize)
    KIDJourney
    KIDJourney
    OP
    Jan 27, 2016
    5
    @bobuick 看到了,多谢了。
    KIDJourney
    KIDJourney
    OP
    Jan 27, 2016
    6
    @bobuick 还是不太明白。。我再去看一下。
    julyclyde
    julyclyde
    Jan 27, 2016
    7
    同问,为什么__get__()
    关注
    phithon
    phithon
    Jan 27, 2016❤️1
    8
    不知道楼主是不是这个意思
    https://gist.github.com/phith0n/afe56234e3301435c3dd
    KIDJourney
    KIDJourney
    OP
    Jan 28, 2016
    9
    @phithon 多谢了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1128 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:54 · PVG 07:54 · LAX 15:54 · JFK 18:54
    ♥ Do have faith in what you're doing.