gpt4 book ai didi

python - pdb中 "next"和 "until"有什么区别

转载 作者:太空狗 更新时间:2023-10-30 01:21:57 25 4
gpt4 key购买 nike

我用的是Python 2.6.6,用pdb调试我的Python程序,但是我不清楚pdb中的“next”和“until”有什么区别,貌似它们都会一直执行到当前函数的下一行。

最佳答案

pdb 帮助文档是这样描述的:

(Pdb) help next
n(ext)
Continue execution until the next line in the current function
is reached or it returns.

(Pdb) help until
unt(il)
Continue execution until the line with a number greater than the current
one is reached or until the current frame returns

更有帮助,Doug Hellman gives an example in his Python Module Tutorial ofthe Week这说明了差异:

The until command is like next, except it explicitly continues until execution reaches a line in the same function with a line number higher than the current value. That means, for example, that until can be used to step past the end of a loop.

pdb_next.py

import pdb

def calc(i, n):
j = i * n
return j

def f(n):
for i in range(n):
j = calc(i, n)
print i, j
return

if __name__ == '__main__':
pdb.set_trace()
f(5)

$ python pdb_next.py
> .../pdb_next.py(21)<module>()
-> f(5)
(Pdb) step
--Call--
> .../pdb_next.py(13)f()
-> def f(n):

(Pdb) step
> .../pdb_next.py(14)f()
-> for i in range(n):

(Pdb) step
> .../pdb_next.py(15)f()
-> j = calc(i, n)

(Pdb) next
> .../pdb_next.py(16)f()
-> print i, j

(Pdb) until
0 0
1 5
2 10
3 15
4 20
> .../pdb_next.py(17)f()
-> return

(Pdb)

Before until was run, the current line was 16, the last line of the loop. After until ran, execution was on line 17, and the loop had been exhausted.

until 的用途与 eponymous gdb command 相同:

until

Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once. It is like the next command, except that when until encounters a jump, it automatically continues execution until the program counter is greater than the address of the jump. This means that when you reach the end of a loop after single stepping though it, until makes your program continue execution until it exits the loop. In contrast, a next command at the end of a loop simply steps back to the beginning of the loop, which forces you to step through the next iteration.

关于python - pdb中 "next"和 "until"有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com