推荐学习书目
› 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 · Dec 24, 2018 · 2213 views
    This topic created in 2837 days ago, the information mentioned may be changed or developed.
    代码很简单:
    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 replies  •  2018-12-25 20:20:22 +08:00
    hxse
        2
    hxse  
       Dec 24, 2018
    你贴的代码没有缩进, 看不懂你的意思
    dlsea
        3
    dlsea  
    OP
       Dec 25, 2018
    代码:
    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
       Dec 25, 2018
    重新输入上面的代码,空格自动被清除了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4127 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 05:38 · PVG 13:38 · LAX 22:38 · JFK 01:38
    ♥ Do have faith in what you're doing.