網(wǎng)站首頁 編程語言 正文
pdb 使用方法
1. 常用命令
a. 斷點(diǎn)設(shè)置
b(reak)
[([filename:]lineno | function) [, condition]]
如果帶有 lineno 參數(shù),則在當(dāng)前文件相應(yīng)行處設(shè)置一個斷點(diǎn)。如果帶有 function 參數(shù),則在該函數(shù)的第一條可執(zhí)行語句處設(shè)置一個斷點(diǎn)。行號可以加上文件名和冒號作為前綴,以在另一個文件(可能是尚未加載的文件)中設(shè)置一個斷點(diǎn)。另一個文件將在 sys.path
范圍內(nèi)搜索。請注意,每個斷點(diǎn)都分配有一個編號,其他所有斷點(diǎn)命令都引用該編號。
如果第二個參數(shù)存在,它應(yīng)該是一個表達(dá)式,且它的計(jì)算值為 true 時斷點(diǎn)才起作用。
如果不帶參數(shù)執(zhí)行,將列出所有中斷,包括每個斷點(diǎn)、命中該斷點(diǎn)的次數(shù)、當(dāng)前的忽略次數(shù)以及關(guān)聯(lián)的條件(如果有)。
tbreak
[([filename:]lineno | function) [, condition]]
臨時斷點(diǎn),在第一次命中時會自動刪除。它的參數(shù)與 break
相同。
cl(ear)
[filename:lineno | bpnumber …]
如果參數(shù)是 filename:lineno,則清除此行上的所有斷點(diǎn)。如果參數(shù)是空格分隔的斷點(diǎn)編號列表,則清除這些斷點(diǎn)。如果不帶參數(shù),則清除所有斷點(diǎn)(但會先提示確認(rèn))。
b. 運(yùn)行
n(ext)
繼續(xù)運(yùn)行,直到運(yùn)行到當(dāng)前函數(shù)的下一行,或當(dāng)前函數(shù)返回為止。( next
和 step
之間的區(qū)別在于,step
進(jìn)入被調(diào)用函數(shù)內(nèi)部并停止,而 next
(幾乎)全速運(yùn)行被調(diào)用函數(shù),僅在當(dāng)前函數(shù)的下一行停止。)
s(tep)
運(yùn)行當(dāng)前行,在第一個可以停止的位置(在被調(diào)用的函數(shù)內(nèi)部或在當(dāng)前函數(shù)的下一行)停下。
c(ont(inue))
繼續(xù)運(yùn)行,僅在遇到斷點(diǎn)時停止。
r(eturn)
繼續(xù)運(yùn)行,直到當(dāng)前函數(shù)返回。
c. 查看
p expression
在當(dāng)前上下文中運(yùn)行 expression 并打印它的值。
print() 也可以使用,但它不是一個調(diào)試器命令 — 它執(zhí)行 Python print()
函數(shù)。
pp expression
與 p
命令類似,但表達(dá)式的值使用 pprint
模塊美觀地打印。
d. 其他
h(elp)
[command]
不帶參數(shù)時,顯示可用的命令列表。參數(shù)為 command 時,打印有關(guān)該命令的幫助。help pdb
顯示完整文檔(即 pdb
模塊的文檔字符串)。由于 command 參數(shù)必須是標(biāo)識符,因此要獲取 !
的幫助必須輸入 help exec
。
q(uit)
退出調(diào)試器。 被執(zhí)行的程序?qū)⒈恢兄埂?/p>
2. 使用方法一
python3 -m pdb test.py
這里程序來自于 無重復(fù)字符的最長子串 。
def lengthOfLongestSubstring(s: str) -> int: st = set() n = len(s) right, result = -1, 0 for left in range(n): if left!=0: st.remove(s[left - 1]) while right + 1 < n and s[right + 1] not in st: st.add(s[right + 1]) right += 1 result = max(result, right - left + 1) return result if __name__=='__main__': print(lengthOfLongestSubstring("abcabcbb"))
輸入 python3 -m pdb test.py
會自動停在第一行
(base) cilinyan@amax:~/clyan/xshell$ python3 -m pdb test.py > /data2/cilinyan/clyan/xshell/test.py(1)<module>() -> def lengthOfLongestSubstring(s: str) -> int:
以下是 python3 -m pdb test.py
的一些使用過程:
(base) cilinyan@amax:~/clyan/xshell$ python3 -m pdb test.py > /data2/cilinyan/clyan/xshell/test.py(1)<module>() -> def lengthOfLongestSubstring(s: str) -> int: (Pdb) ll 1 -> def lengthOfLongestSubstring(s: str) -> int: 2 st = set() 3 n = len(s) 4 right, result = -1, 0 5 for left in range(n): 6 if left!=0: st.remove(s[left - 1]) 7 while right + 1 < n and s[right + 1] not in st: 8 st.add(s[right + 1]) 9 right += 1 10 result = max(result, right - left + 1) 11 return result 12 13 if __name__=='__main__': 14 print(lengthOfLongestSubstring("abcabcbb")) (Pdb) b 5 Breakpoint 1 at /data2/cilinyan/clyan/xshell/test.py:5 (Pdb) ll 1 def lengthOfLongestSubstring(s: str) -> int: 2 st = set() 3 n = len(s) 4 right, result = -1, 0 5 B-> for left in range(n): 6 if left!=0: st.remove(s[left - 1]) 7 while right + 1 < n and s[right + 1] not in st: 8 st.add(s[right + 1]) 9 right += 1 10 result = max(result, right - left + 1) 11 return result (Pdb) p right -1 (Pdb) p s 'abcabcbb'
3. 使用方法二
import pdb; pdb.set_trace()
這里程序來自于 無重復(fù)字符的最長子串 。
import pdb def lengthOfLongestSubstring(s: str) -> int: st = set() n = len(s) right, result = -1, 0 pdb.set_trace() for left in range(n): if left!=0: st.remove(s[left - 1]) while right + 1 < n and s[right + 1] not in st: st.add(s[right + 1]) right += 1 result = max(result, right - left + 1) pdb.set_trace() return result if __name__=='__main__': print(lengthOfLongestSubstring("abcabcbb"))
輸入 python test.py
會自動設(shè)置 pdb.set_trace()
的下一行,以下是一些使用過程:
(base) cilinyan@amax:~/clyan/xshell$ python test.py > /data2/cilinyan/clyan/xshell/test.py(8)lengthOfLongestSubstring() -> for left in range(n): (Pdb) n > /data2/cilinyan/clyan/xshell/test.py(9)lengthOfLongestSubstring() -> if left!=0: st.remove(s[left - 1]) (Pdb) n > /data2/cilinyan/clyan/xshell/test.py(10)lengthOfLongestSubstring() -> while right + 1 < n and s[right + 1] not in st: (Pdb) c > /data2/cilinyan/clyan/xshell/test.py(15)lengthOfLongestSubstring() -> return result (Pdb) c 3 (base) cilinyan@amax:~/clyan/xshell$
原文鏈接:https://blog.csdn.net/weixin_43791477/article/details/125238550
相關(guān)推薦
- 2022-07-11 python連接clickhouse的端口問題及解決_python
- 2022-07-08 一文詳解C++中運(yùn)算符的使用_C 語言
- 2022-06-11 C#實(shí)現(xiàn)文件Move和Copy操作_C#教程
- 2023-06-04 django數(shù)據(jù)庫報錯解決匯總:django.db.utils.OperationalError?1
- 2022-06-15 Go?interface?接口的最佳實(shí)踐經(jīng)驗(yàn)分享_Golang
- 2022-04-01 C#中逆變的實(shí)際應(yīng)用場景詳解_C#教程
- 2022-08-22 python深度學(xué)習(xí)tensorflow1.0參數(shù)初始化initializer_python
- 2022-12-13 Python使用自定義裝飾器的示例詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支