python 如何调试函数_使用函数 - python断点调试方法

清单 7. 使用函数的例子

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 退出到调用的地方。

清单 8. 对函数进行 debug

[root@rcc-pok-idg-2255 ~]# python epdb2.py

》 /root/epdb2.py(10)?()

-》 b = “bbb”

(Pdb) n

》 /root/epdb2.py(11)?()

-》 c = “ccc”

(Pdb) n

》 /root/epdb2.py(12)?()

-》 final = combine(a,b)

(Pdb) s

--Call--

》 /root/epdb2.py(3)combine()

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

(Pdb) n

》 /root/epdb2.py(4)combine()

-》 s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。

(Pdb) list

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”

(Pdb) n

》 /root/epdb2.py(5)combine()

-》 s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。

(Pdb) n

》 /root/epdb2.py(6)combine()

-》 return s3 # and returns it.

(Pdb) n

--Return--

》 /root/epdb2.py(6)combine()-》‘“aaabbbaaa”’

-》 return s3 # and returns it.

(Pdb) n

》 /root/epdb2.py(13)?()

-》 print final

(Pdb)

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

清单 9. 在调试的时候动态改变值

[root@rcc-pok-idg-2255 ~]# python epdb2.py

》 /root/epdb2.py(10)?()

-》 b = “bbb”

(Pdb) var = “1234”

(Pdb) b = “avfe”

*** The specified object ‘= “avfe”’ is not a function

or was not found along sys.path.

(Pdb) !b=“afdfd”

(Pdb)

pdb 调试有个明显的缺陷就是对于多线程,远程调试等支持得不够好,同时没有较为直观的界面显示,不太适合大型的 python 项目。而在较大的 python 项目中,这些调试需求比较常见,因此需要使用更为高级的调试工具。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值