V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-01-27 15:24:22 +08:00 · 3626 次点击
    这是一个创建于 3027 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个是装饰器类。

    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 条回复    2016-01-28 00:29:04 +08:00
    kinghui
        1
    kinghui  
       2016-01-27 15:25:50 +08:00
    ```python
    @rediscache.PostCache()
    def __get_content_list(self, url_list):
    ....
    ```
    bobuick
        2
    bobuick  
       2016-01-27 15:27:36 +08:00   ❤️ 1
    你需要确保它实现了 __call__() 和 __get__() 方法
    kinghui
        3
    kinghui  
       2016-01-27 15:28:10 +08:00
    看错了, 请忽略我
    KIDJourney
        4
    KIDJourney  
    OP
       2016-01-27 15:30:39 +08:00
    @bobuick 为什么要实现__get__()呢?
    [PythonDecoratorLibrary]( https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize)
    KIDJourney
        5
    KIDJourney  
    OP
       2016-01-27 15:31:18 +08:00
    @bobuick 看到了,多谢了。
    KIDJourney
        6
    KIDJourney  
    OP
       2016-01-27 15:35:15 +08:00
    @bobuick 还是不太明白。。我再去看一下。
    julyclyde
        7
    julyclyde  
       2016-01-27 20:51:39 +08:00
    同问,为什么__get__()
    关注
    phithon
        8
    phithon  
       2016-01-27 22:46:37 +08:00   ❤️ 1
    不知道楼主是不是这个意思
    https://gist.github.com/phith0n/afe56234e3301435c3dd
    KIDJourney
        9
    KIDJourney  
    OP
       2016-01-28 00:29:04 +08:00
    @phithon 多谢了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5534 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:36 · PVG 15:36 · LAX 00:36 · JFK 03:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.