推荐学习书目
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 · 4195 views
    This topic created in 3840 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
        1
    kinghui  
       Jan 27, 2016
    ```python
    @rediscache.PostCache()
    def __get_content_list(self, url_list):
    ....
    ```
    bobuick
        2
    bobuick  
       Jan 27, 2016   ❤️ 1
    你需要确保它实现了 __call__() 和 __get__() 方法
    kinghui
        3
    kinghui  
       Jan 27, 2016
    看错了, 请忽略我
    KIDJourney
        4
    KIDJourney  
    OP
       Jan 27, 2016
    @bobuick 为什么要实现__get__()呢?
    [PythonDecoratorLibrary]( https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize)
    KIDJourney
        5
    KIDJourney  
    OP
       Jan 27, 2016
    @bobuick 看到了,多谢了。
    KIDJourney
        6
    KIDJourney  
    OP
       Jan 27, 2016
    @bobuick 还是不太明白。。我再去看一下。
    julyclyde
        7
    julyclyde  
       Jan 27, 2016
    同问,为什么__get__()
    关注
    phithon
        8
    phithon  
       Jan 27, 2016   ❤️ 1
    不知道楼主是不是这个意思
    https://gist.github.com/phith0n/afe56234e3301435c3dd
    KIDJourney
        9
    KIDJourney  
    OP
       Jan 28, 2016
    @phithon 多谢了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2761 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:22 · PVG 20:22 · LAX 05:22 · JFK 08:22
    ♥ Do have faith in what you're doing.