python中pdb的使用教程

1.运行脚本至断点pdb.set_trace()处,n+enter/enter执行当前的statement

2.推出debug:quit/q,暴力退出

3.打印变量的值:p 变量A(条件是A已经执行得到)

4.停止debug继续执行程序:c

5.debug过程中显示代码:list/l

6.使用函数:

import pdb def combine(s1,s2): # define subroutine combine, which... s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ... s3 = '"' + s3 +'"' # encloses it in double quotes,... return s3 # and returns it. a = "aaa" pdb.set_trace() b = "bbb" c = "ccc" final = combine(a,b) print final

1

2

3

4

5

6

7

8

9

10

11

import pdb

def combine(s1,s2):      # define subroutine combine, which...

    s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ...

    s3 = '"' + s3 +'"'   # encloses it in double quotes,...

    return s3            # and returns it.

a = "aaa"

pdb.set_trace()

b = "bbb"

c = "ccc"

final = combine(a,b)

print final

如果直接使用 n 进行 debug 则到 final=combine(a,b) 这句的时候会将其当做普通的赋值语句处理,进入到 print final。如果想要对函数进行 debug 如何处理呢 ? 可以直接使用 s 进入函数块。函数里面的单步调试与上面的介绍类似。如果不想在函数里单步调试可以在断点处直接按 r 退出到调用的地方。

7.在调试的时候动态改变值

在调试的时候动态改变值 。在调试的时候可以动态改变变量的值,具体如下实例。需要注意的是下面有个错误,原因是 b 已经被赋值了,如果想重新改变 b 的赋值,则应该使用!b

猜你喜欢

转载自blog.csdn.net/dinry/article/details/83656015