Python pdb按条件设置断点

使用了一段时间pdb调试,但是都是一步一步调试,这次循环比较多,就有了按条件设置断点的需求了。pdb中按条件设置断点的方法有以下两种。

第一种是在pdb环境中设置:

andrew@ubuntu:~/PycharmProjects/Algotiths$ python find_kth_smallest.py
> /home/andrew/PycharmProjects/Algotiths/find_kth_smallest.py(23)<module>()
-> print("the {}th min num is {}".format(k,find_kth_smallest_num(l,k)))
(Pdb) s
--Call--
> /home/andrew/PycharmProjects/Algotiths/find_kth_smallest.py(3)find_kth_smallest_num()
-> def find_kth_smallest_num(l,k):
(Pdb) l
  1      
  2      
  3  ->    def find_kth_smallest_num(l,k):
  4          s=[]
  5          for i in range(k):
  6              res=float('inf')
  7              for item in l:
  8                  if res>item:
  9                      res,item=item,res
 10              if i!=k-1:
 11                  l.remove(res)
(Pdb) b 10,res==2  # 其中10 是断点设置的代码行,res==2是断点条件
Breakpoint 1 at /home/andrew/PycharmProjects/Algotiths/find_kth_smallest.py:10
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /home/andrew/PycharmProjects/Algotiths/find_kth_smallest.py:10
    stop only if res==2
(Pdb) c
> /home/andrew/PycharmProjects/Algotiths/find_kth_smallest.py(10)find_kth_smallest_num()
-> if i!=k-1:
(Pdb) p res
2

第二种是在代码中设置:

andrew@ubuntu:~/PycharmProjects/Algotiths$ python find_kth_smallest_pdb2.py
> /home/andrew/PycharmProjects/Algotiths/find_kth_smallest_pdb2.py(13)find_kth_smallest_num()
-> if i!=k-1:
(Pdb) p res
2
(Pdb) b
(Pdb) l
  8                  if res>item:
  9                      res,item=item,res
 10              if res==2:     # 断点条件
 11                  import pdb
 12                  pdb.set_trace()  
 13  ->            if i!=k-1:
 14                  l.remove(res)
 15              else:
 16                  return l[0]
 17      
 18      
(Pdb) 

 

参考:

pdb/ipdb for python break on editable condition

conditional breakpoint using pdb

转载于:https://www.cnblogs.com/xieshiqing/p/Python_pdb_condition.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值