推荐学习书目
› 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
sasic
V2EX  ›  Python

装饰器问题

  •  
  •   sasic · Mar 21, 2019 via iPhone · 2228 views
    This topic created in 2750 days ago, the information mentioned may be changed or developed.
    关于 Python 装饰器问题
    想在同一 class 下,用方法 A 装饰方法 B,功能比如说:在 B 执行完,print 属性 C 可行吗?
    4 replies  •  2019-03-26 18:14:55 +08:00
    sasic
        1
    sasic  
    OP
       Mar 21, 2019 via iPhone
    class school():
    def __init__(self, c):
    self.c=c
    def A
    def B
    aijam
        2
    aijam  
       Mar 22, 2019
    yes and no。这只是一个 anti-pattern workaround,注意这里的 decorator 不是 method。虽然也可以定义成 method,但是需要更多的 hack。
    https://gist.github.com/czheo/a976c92f60c6757f6e9dd316174eea6f
    sasic
        3
    sasic  
    OP
       Mar 22, 2019
    @aijam 非常感谢,可以举个例吗?网址好像打不开
    shyrock
        4
    shyrock  
       Mar 26, 2019
    是不是这样?
    class school():
    def __init__(self,c):
    self.c = c
    def A(func):
    def dec(self):
    result = func(self)
    print(self.c)
    return result
    return dec
    @A
    def B(self):
    print('exec B()')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2414 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:14 · PVG 14:14 · LAX 23:14 · JFK 02:14
    ♥ Do have faith in what you're doing.