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
HxSeek
V2EX  ›  Python

话说 Python 官网里面有详细讲装饰器( decorator)吗?

  •  
  •   HxSeek · 2015-01-02 23:33:06 +08:00 · 3125 次点击
    这是一个创建于 3399 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题··,我在官方文档内(2.X)版本那·· 查找不到···请问是没有?还是我找不到而已。 如果官方文档没有··大家是看什么入门decorator的?
    8 条回复    2015-01-03 21:01:17 +08:00
    xiandao7997
        1
    xiandao7997  
       2015-01-02 23:46:13 +08:00 via Android
    不懂 decorator, 不过 lz 的问题可以搜索 decorator site:python.org :)
    另,找到一个教程, http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html
    binux
        2
    binux  
       2015-01-02 23:54:25 +08:00
    https://docs.python.org/2/reference/compound_stmts.html#function-definitions
    你还要多详细,本来就没多少东西,就一语法糖
    yourmoonlight
        3
    yourmoonlight  
       2015-01-03 09:07:00 +08:00
    @f1(arg)
    @f2
    def func(): pass
    is equivalent to:

    def func(): pass
    func = f1(arg)(f2(func))
    zonyitoo
        4
    zonyitoo  
       2015-01-03 11:13:57 +08:00
    decorator就是一个语法糖来的而已呀
    yourmoonlight
        5
    yourmoonlight  
       2015-01-03 13:41:17 +08:00
    python decorators are equated to macros.

    the goal of macros is to provide a way to modify elements of the language.

    that's what decorators do in python, they modify functions, in the case of class decorators, entire classes.
    rcmerci
        6
    rcmerci  
       2015-01-03 14:03:53 +08:00   ❤️ 1
    的确只是语法糖而已,nothing more
    HxSeek
        7
    HxSeek  
    OP
       2015-01-03 15:35:25 +08:00
    原来如此··小白就此谢过大家的回复啦··
    clvrobj
        8
    clvrobj  
       2015-01-03 21:01:17 +08:00
    虽然lz已经原来如此了,还是留下个觉得有用的链接 https://www.python.org/dev/peps/pep-0318/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:03 · PVG 10:03 · LAX 19:03 · JFK 22:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.