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

断点调试装饰器遇到的疑问疑问🤔️

  •  
  •   Litecoins · 2019-01-19 21:53:44 +08:00 · 1881 次点击
    这是一个创建于 1895 天前的主题,其中的信息可能已经有所发展或是发生改变。

    向大家请教一个问题哈,我在第 11 行设置了断点,为什么程序还是运行了 11 行之后的代码呢?

    8 条回复    2019-01-20 09:58:15 +08:00
    xpresslink
        1
    xpresslink  
       2019-01-19 22:01:25 +08:00
    因为装饰器是在编译字节码的阶段就执行生成了,说白了装饰器只是语法糖。
    megachweng
        2
    megachweng  
       2019-01-19 23:54:32 +08:00 via iPhone
    function decorators are executed as soon as the module is imported, but the decorated functions only run when they are explicitly invoked
    lance6716
        3
    lance6716  
       2019-01-20 00:18:40 +08:00 via Android
    Python 这种轻松的语言竟然还有调试器…断点打在这里不能理解是想在什么时候停止,应该打在 12 行吧
    GeruzoniAnsasu
        4
    GeruzoniAnsasu  
       2019-01-20 00:55:01 +08:00
    @xpresslink 是语法糖,但装饰器相当于一个赋值语句,在执行流中的位置还是比较明确的

    @register 相当于 f1 = register(f1),执行到这行时,在装饰器函数内部下断应该是可以断下来的

    在 12 行下断只有在执行 f1()时才会中断
    Litecoins
        5
    Litecoins  
    OP
       2019-01-20 09:57:25 +08:00
    @xpresslink 明白了,谢谢!
    Litecoins
        6
    Litecoins  
    OP
       2019-01-20 09:57:38 +08:00
    @megachweng 明白了,谢谢!
    Litecoins
        7
    Litecoins  
    OP
       2019-01-20 09:57:57 +08:00
    @lance6716 明白了,感谢!
    Litecoins
        8
    Litecoins  
    OP
       2019-01-20 09:58:15 +08:00
    @GeruzoniAnsasu 明白了,感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2839 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.