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

Python pdb.set_trace()问题

  •  
  •   dlsea · 2018-12-24 11:17:48 +08:00 · 1439 次点击
    这是一个创建于 1921 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码很简单:
    import pdb
    def sum_nums(n):
    s = 0
    for i in range(n):
    pdb.set_trace()
    s += i
    print(s)
    if __name__ == '__main__':
    sum_nums(5)

    执行代码:
    > /root/1.py(7)sum_nums()
    -> s += i
    (Pdb) c
    0
    > /root/1.py(6)sum_nums()
    -> pdb.set_trace()
    (Pdb) c
    1
    > /root/1.py(7)sum_nums()
    -> s += i
    (Pdb) c
    3
    > /root/1.py(6)sum_nums()
    -> pdb.set_trace()
    (Pdb) c
    6
    > /root/1.py(7)sum_nums()
    -> s += i
    (Pdb) c
    10

    如果把代码改为:
    for i in range(n):
    s += i
    print(s)
    pdb.set_trace()
    那么执行后的每一条待执行都是-> for i in range(n):

    疑问:
    所有待执行的语句不应该都是 -> s += i 吗?为什么还穿插着 -> pdb.set_trace() ?
    4 条回复    2018-12-25 20:20:22 +08:00
    jdhao
        1
    jdhao  
       2018-12-24 13:11:25 +08:00
    hxse
        2
    hxse  
       2018-12-24 21:50:47 +08:00
    你贴的代码没有缩进, 看不懂你的意思
    dlsea
        3
    dlsea  
    OP
       2018-12-25 20:18:48 +08:00
    代码:
    import pdb

    def sum_nums(n):
    s = 0
    for i in range(n):
    pdb.set_trace()
    s += i
    print(s)
    if __name__ == '__main__':
    sum_nums(5)
    dlsea
        4
    dlsea  
    OP
       2018-12-25 20:20:22 +08:00
    重新输入上面的代码,空格自动被清除了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5288 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:28 · PVG 17:28 · LAX 02:28 · JFK 05:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.